如何调试从 C# 执行的 powershell 脚本? [英] How do I debug a powershell script executed from C#?

查看:38
本文介绍了如何调试从 C# 执行的 powershell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 C# 中的 System.Management.Automation 命名空间运行了一个 powershell 脚本,类似于下面的代码.

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.

有没有一种方法,无需进入powershell脚本并每2行放置一个Write-Host $somevariable,来逐步调试这个脚本?

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?

我应该提到脚本本身不能独立运行,C# 代码向脚本的运行空间添加了变量.

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

推荐答案

可以使用脚本 添加调试器.ps1.UI 非常原始,只有一个输入对话框和一个输出控制台,您必须在脚本中添加一些临时代码才能进行调试.但是如果没有更好的选择,这样的调试器仍然可以很好地工作.

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.

这篇关于如何调试从 C# 执行的 powershell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆