简体   繁体   English

如何调试从C#执行的powershell脚本?

[英]How do I debug a powershell script executed from C#?

I have code running a powershell Script though the use of the System.Management.Automation namespace in C#, similar to the code below. 我通过在C#中使用System.Management.Automation命名空间运行PowerShell脚本的代码,类似于下面的代码。

 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? 有没有办法,没有进入powershell脚本并每两行放一个Write-Host $somevariable ,一步一步调试这个脚本?

I should mention the script itself cannot run standalone, the C# code adds variables to the runspace of the script. 我应该提一下脚本本身不能独立运行,C#代码将变量添加到脚本的运行空间中。

It is possible with a script Add-Debugger.ps1 . 可以使用脚本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. UI非常原始,只是一个输入对话框和一个输出控制台,您必须在脚本中添加一些临时代码才能进行调试。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM