4

I have code running a powershell Script though the use of the System.Management.Automation namespace in C#, similar to the code below.

 using (mPowershell = PowerShell.Create()) 
 {
        mPowershell.AddScript(GetScriptText(mStep), true);
        SetPowershellVariables(mPowershell);
        output = new PSDataCollection<PSObject>();
        output.DataAdded += new EventHandler<DataAddedEventArgs>(Output_DataAdded);
        mPowershell.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(Powershell_InvocationStateChanged);
        IAsyncResult asyncResult = mPowershell.BeginInvoke<PSObject, PSObject>(null, output);      
}

As expected I am getting errors, and I would like to debug them.

Is there a way, without going into the powershell script and putting a Write-Host $somevariable every 2 lines, to step by step debug this script?

I should mention the script itself cannot run standalone, the C# code adds variables to the runspace of the script.

3
  • 1
    Use the Integrated Scripting Environment? Commented Sep 24, 2015 at 1:01
  • Maybe "attach to process" with debugger could be a way...not sure if and how the debugger would find the source code for your script, though... Maybe a DebugBreak()...somewhere?
    – BitTickler
    Commented Sep 24, 2015 at 5:17
  • Instead of C# adding variables to the script, make those things script parameters; then you'll be able to work with the script standalone. Commented Sep 26, 2015 at 3:31

1 Answer 1

3

It is possible with a script Add-Debugger.ps1. The UI is very primitive, just an input dialog box and a console for output, and you have to add some temporary code to your script in order to debug. But such a debugger still does the job fine if there is no better options.

The temporary code to add to a script in order to add and trigger debugging

# Add debugger with file output shown in a separate console.
# <path\> may be omitted if Add-Debugger.ps1 is in the path.

<path\>Add-Debugger.ps1 $env:TEMP\debug.log

# set some breakpoints
$null = Set-PSBreakpoint ...

From now on when a breakpoint is triggered a debugger input box is shown. Type commands, examine variables, watch the output in the output console. The set of available commands:

s, StepInto  Step to the next statement into functions, scripts, etc.
v, StepOver  Step to the next statement over functions, scripts, etc.
o, StepOut   Step out of the current function, script, etc.
c, Continue  Continue operation (also on empty input).
q, Quit      Stop operation and exit the debugger.
?, h         Write this help message.
k            Write call stack (Get-PSCallStack).
K            Write detailed call stack using Format-List.

<n>          Write debug location in context of <n> lines.
+<n>         Set location context preference to <n> lines.
k <s> <n>    Write source at stack <s> in context of <n> lines.

w            Restart watching the debugger output file.
r            Write last PowerShell commands invoked on debugging.
<command>    Invoke any PowerShell <command> and write its output.

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.