Hi folks,
I’m playing with the remote debugging in powershell v5(preview) console by doing below, and it just works perfectly to me, the debugger breaks into the bp I set for the remote session.
PS C:\Windows\system32> Enter-PSSession -ComputerName localhost
[localhost]: PS C:\Users\zhiyd\Documents> Set-PSBreakpoint -Line 5 -Script C:\test\test1.ps1
ID Script Line Command Variable Action
0 test1.ps1 5
[localhost]: PS C:\Users\zhiyd\Documents> . “C:\test\test1.ps1”
Hit Line breakpoint on ‘C:\test\test1.ps1:5’
At C:\test\test1.ps1:5 char:33
-
$DateTime = Get-Date;
-
~~~~~~~~~~~~~~~~~~~~~~~
[localhost]: [DBG]: PS C:\Users\zhiyd\Documents>>
Now I’m trying to utilize the debugger API to do the similar thing for the my own PSHost(which in the below case, it is a console app), and I’m having trouble to have the debugger break into the breakpoint I set, here is the code snippet from my console app:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) <-----------please note that this is a remote runspace
{
runspace.Open();
runspace.Debugger.SetDebugMode(DebugModes.RemoteScript); <-----------tried with different options like DebugModes.Default or None, none of them helps
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
runspace.Debugger.BreakpointUpdated += HandlerBreakpointUpdatedEvent;
runspace.Debugger.DebuggerStop += HandleDebuggerStopEvent; <---------------I expect this callback can be hit when the script file(with bp set) was invoked, but it was never hit
// Set initial breakpoint on line 10 of script. This breakpoint
// will be in the script workflow function.
powerShell.AddCommand("Set-PSBreakpoint").AddParameter("Script", "C:\test\test1.ps1").AddParameter("Line", 5);
powerShell.Invoke();
Console.WriteLine("Starting script file: " + filePath);
Console.WriteLine();
// Run script file.
powerShell.Commands.Clear();
powerShell.AddScript("C:\test\test1.ps1").AddCommand("Out-String").AddParameter("Stream", true);
var scriptOutput = new PSDataCollection();
scriptOutput.DataAdded += (sender, args) =>
{
// Stream script output to console.
foreach (var item in scriptOutput.ReadAll())
{
Console.WriteLine(item);
}
};
powerShell.Invoke(null, scriptOutput);
}
}
As a reference, the above code snippet is from the debugger API sample code on msdn at https://code.msdn.microsoft.com/windowsdesktop/windows-powershell-f5c03f2d/sourcecode?fileid=98475&pathid=297240411, the only difference is that instead of doing an in-proc local runspace, my code is trying to create a remote runspace to localhost. This sample works fine(meaning bp can be hit and callback can be triggered) with local runspace, but here I would like to make it work for remote runspace, coz obviously it works on powershell console(v5) as I mentioned at the beginning.
Is there anything wrong or missed from my above implementation?
Thanks and appreciate with you help!