Debugging other PowerShell processes
A PowerShell developer can make use of Enter-PSHostProcess
to debug a script running in a different PowerShell process.
The Enter-PSHostProcess
command creates a remote runspace inside an existing PowerShell process.
Demonstrating use requires two PowerShell processes. The first PowerShell process is started, and for convenience, the PID
of that process is shown.
A simple infinite loop is started after setting the window title to show the current PID
:
$host.UI.RawUI.WindowTitle = 'Script runner: {0}' -f $PID
$i = 0
while ($true) {
Write-Host "Working on $i"
$i++
Start-Sleep -Seconds 30
}
In the example that follows, the first process showed a PID
of 14504
and is used to connect to that process. This PID
will be different in each case.
A second process is started and the Enter-PSHostProcess
command is used to connect to the original PID
. The prompt will change to reflect the connection to...