You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#编写PowerShell Cmdlet:如何在同一实例中执行脚本?

解决方案:在自定义Cmdlet中执行同实例PowerShell脚本

1. 利用Cmdlet内置的InvokeCommand.InvokeScript方法

这是最直接的解决方式,无需手动创建PowerShell实例,直接通过Cmdlet的上下文执行脚本,不会触发管道繁忙错误。

示例代码:

protected override void ProcessRecord()
{
    // 定义添加Test-Function1的脚本
    var script = @"
        function Test-Function1 {
            param($Message)
            Write-Host ""Test-Function1 called with: $Message""
        }
    ";

    // 通过InvokeCommand执行脚本
    this.InvokeCommand.InvokeScript(script);

    // 测试调用刚添加的函数
    this.InvokeCommand.InvokeScript("Test-Function1 -Message 'Hello from Show-XamlWindow'");

    // 此处编写你的XAML窗口逻辑
    // ...
}

2. 异步执行脚本(适用于后台运行场景)

如果必须创建独立PowerShell实例执行脚本,需要在单独线程中异步调用,避免阻塞当前正在处理Cmdlet的管道。

示例代码:

protected override void ProcessRecord()
{
    var currentRunspace = Runspace.DefaultRunspace;

    // 在新线程中异步执行脚本
    Task.Run(() =>
    {
        using (var ps = PowerShell.Create())
        {
            ps.Runspace = currentRunspace;
            ps.AddScript(@"
                function Test-Function1 {
                    param($Message)
                    Write-Host ""Test-Function1 called with: $Message""
                }
            ");
            
            var executionResult = ps.Invoke();

            // 处理执行过程中的错误
            if (ps.HadErrors)
            {
                foreach (var error in ps.Streams.Error)
                {
                    WriteError(new ErrorRecord(error.Exception, "ScriptExecutionFailed", ErrorCategory.SyntaxError, null));
                }
            }
        }
    });

    // 此处编写你的XAML窗口逻辑
    // ...
}

关键注意事项

  • 禁止在Cmdlet的ProcessRecord同步线程中,直接创建PowerShell实例并绑定DefaultRunspace执行同步调用——当前管道正处于处理Cmdlet的状态,同步操作必然触发“管道繁忙”错误。
  • 如果需要在XAML窗口的事件回调(如按钮点击)中执行脚本,同样要使用异步方式,确保回调逻辑不运行在Cmdlet的执行线程中。
  • 使用InvokeCommand.InvokeScript时,可通过参数传递变量,示例:
var paramDict = new Dictionary<string, object>
{
    { "Message", "参数化调用测试" }
};
this.InvokeCommand.InvokeScript(@"
    param($Message)
    Write-Host $Message
", paramDict);

内容的提问来源于stack exchange,提问作者Enrico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:37:49