5

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:

$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.

1 Answer 1

7

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:

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:

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:

> 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:

> Get-PSSession | Connect-PSSession

 Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
  9 Session8        localhost       Opened        Microsoft.PowerShell   RemoteDebug

And enter the session:

> 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

[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 <m>"
                     to start from line <m>, and "list <m> <n>" to list <n>
                     lines starting from line <m>

 <enter>             Repeat last command if it was stepInto, stepOver or list

 ?, 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>>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help, but when I try it with my code that is a bit more complicated, I get the following error message: Breakpoints cannot be set in the remote session because remote debugging is not supported by the current host. + CategoryInfo : NotImplemented: (:) [Set-PSBreakpoint], PSNotSupportedException + FullyQualifiedErrorId : SetPSBreakpoint:RemoteDebuggerNotSupported,Microsoft.PowerShell.Commands.SetPSBreakpointCommand Although I'm also using PowerShell 4.0 on a Windows 2008 R2 Server on the localhost.
This was an error coming from the PowerShell ISE. After trying it in the normal PowerShell Console it is indeed telling me there is an open session as you have. But after several Invoke-Commands that come after the Set-PSBreakpoint the Get-PSSession CmdLet doesn't give me any open session anymore to connect to..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.