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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$session = New-PSSession -ComputerName localhost Invoke-Command -Session $session -ScriptBlock $LoadFunctions # Do other stuff Invoke-Command -Session $session -ScriptBlock { $Time = Get-Date $Var = '5' # Stop debugger here Set-PSBreakpoint -Variable $Var } Remove-PSSession $Session |
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:
1 2 3 4 5 6 7 |
Invoke-Command -ComputerName . -ScriptBlock { Set-PSBreakpoint -Variable metoo; $test="foo"; $one="1"; $metoo="metoo"; } -Credential (Get-Credential) |
When I ran this I got the following message:
1 2 3 4 5 |
WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 on computer localhost has been disconnected because the script running on the session has stopped at a breakpoint. Use the Enter-PSSession cmdlet on this session to connect back to the session and begin interactive debugging. WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 has been created for reconnection. |
So to see the session was still there, I did a Get-PSSession:
1 2 3 4 5 6 |
> Get-PSSession Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 9 Session8 localhost Disconnected Microsoft.PowerShell None |
Great, session is there, just need to reconnect and enter:
1 2 3 4 5 6 |
> Get-PSSession | Connect-PSSession Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 9 Session8 localhost Opened Microsoft.PowerShell RemoteDebug |
And enter the session:
1 2 3 4 5 6 7 8 9 10 11 12 |
> Get-PSSession | Enter-PSSession WARNING: You have entered a session that is currently stopped at a debug breakpoint inside a running command or script. Use the Windows PowerShell command line debugger to continue debugging. Entering debug mode. Use h or ? for help. Hit Variable breakpoint on ':$metoo' (Write access) At line:1 char:59 + Set-PSBreakpoint -Variable metoo; $test="foo"; $one="1"; $metoo="metoo"; + ~ [localhost]: [DBG]: PS C:\Users\Foo\Documents>> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[localhost]: [DBG]: PS C:\Users\Foo\Documents>> h s, stepInto Single step (step into functions, scripts, etc.) v, stepOver Step to next statement (step over functions, scripts, etc.) o, stepOut Step out of the current function, script, etc. c, continue Continue operation q, quit Stop operation and exit the debugger k, Get-PSCallStack Display call stack l, list List source code for the current script. Use "list" to start from the current line, "list to start from line lines starting from line ?, h displays this help message. For instructions about how to customize your debugger prompt, type "help about_prompt". [localhost]: [DBG]: PS C:\Users\Foo\Documents>> |