2016-11-29 217 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();