2016-11-29 164 views
0

我使用System.Management.Automation.PowerShell以编程方式从C#应用程序执行PowerShell脚本。 PowerShell脚本加载一个用于执行其活动的.Net dll。调试 - 通过API执行PowerShell脚本执行通过API

var script = "Add-Type -Path 'MyLibrary.dll'; ...."; 

using (var powershell = PowerShell.Create()) { 
    powershell.AddScript(script); 

    powershell.Invoke(); 
} 

是否有可能Visual Studio的调试器莫名其妙连接到PowerShell的对象实例,以便调试器可以无缝地从C#应用程序步入PowerShell脚本,并从脚本到MyLibrary.dll(假设我有一个符号该DLL)?

编辑:基于以下内容,似乎可能没有办法在Visual Studio中将调试从C#无缝地流到PowerShell。但是,可以使用VS来调试由PowerShell启动并启动的C#代码。

+1

你的意思是你想使用Windows PowerShell调试API这样的样本? https://code.msdn.microsoft.com/windowsapps/Windows-PowerShell-f5c03f2d。要调试dll文件,你可以考虑使用自定义帮助器类:http://stackoverflow.com/questions/8062407/debug-c-sharp-dll-when-loaded-into-powershells-process-is-it-甚至可能 –

+0

@ JackZhai-MSFT,感谢那些有用的链接!使用'System.Diagnostics.Debugger.Launch();'(在第二个链接中提到)允许我在Visual Studio中调试由PowerShell调用的.Net代码。我希望有一种方法可以对PowerShell脚本本身做同样的事情,但是哦。 :-) –

+0

@ JackZhai-MSFT,你会介意将你的评论(至少是与System.Diagnostics.Debugger.Launch();')有关的部分转换为答案,以便我可以接受它。 :-) –

回答

1

你可以通过调用调试DLL文件中的助手类以下内容:

System.Diagnostics.Debugger.Launch();