Search code examples
debuggingpowershellremote-debugging

PowerShell Debug Invoke-Command


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.


Solution

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