PowerShell Debug Invoke-Command

Question:

I’m trying to find an easy way to peak inside the ScriptBlock of Invoke-Command. I’ve read the Hey Scripting Guy blog but they only invoke script files and I’m a bit confused at how this can be done.

Can someone tell me how I can have the debugger stop at say the line $Var = 5? I’ve tried with Set-PSBreakpoint, but it always fails. This would be convenient for checking the value and if all the code is correct.

Code example:

Thank you for your help.

Answer:

Here’s what worked for me. I’m in PowerShell 4.0 by the way.

First, I put the set-psbreakpoint earlier in the scriptblock:

When I ran this I got the following message:

So to see the session was still there, I did a Get-PSSession:

Great, session is there, just need to reconnect and enter:

And enter the session:

Great, so now I’m in my remote-debug session! Just follow the prompts, such as typing “h” for help or “k” to get-psscallstack etc

Source:

PowerShell Debug Invoke-Command by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply