Debugging Other PowerShell Processes
Beginning with PowerShell 5.0, the PowerShell ISE can connect to other processes that run a PowerShell runspace, display the source code, and debug the foreign process. This is great news because now you can debug PowerShell code that runs hidden as scheduled task, or within an application such as SCCM.
Here are the basic steps:
# get the process (or process ID) of the
# process you want to debug
# (this assumes that another PowerShell console runs)
$p = Get-Process powershell | Select-Object -First 1
# connect to the process:
Enter-PSHostProcess -Process $p
# connect to a runspace inside this process
# typically, ID 1 is correct, but there may be more
# use Get-Runspace to see all runspaces and their IDs
Debug-Runspace -Id 1
Provided that the connected process currently runs a PowerShell script, the PowerShell ISE invokes the debugger, shows the source code, and allows you to set breakpoints and step through the script as if it was a local script.
If the process is executing interactive content, no source code is shown. If the process is idle, nothing spectacular happens.
ReTweet this Tip!