gpt4 book ai didi

powershell - 在 .NetCore 应用程序中运行 PS 时出错 - 无法执行操作,因为运行空间不在 'Opened' 状态。运行空间的当前状态是 'Broken'

转载 作者:行者123 更新时间:2023-12-05 06:09:44 26 4
gpt4 key购买 nike

我正在尝试实现需要运行 PowerShell 脚本的 .NetCore 3.1 控制台应用程序。但是,当我尝试运行它时,出现以下错误 -

Cannot perform operation because the runspace is not in the 'Opened' state. Current state of runspace is 'Broken'.

运行PS脚本的代码如下:

        using System.Management.Automation;

public async Task<string> RunScript(string scriptToRun)
{
// create a new hosted PowerShell instance using the default runspace.
using PowerShell ps = PowerShell.Create();

// specify the script code to run.
ps.AddScript(scriptToRun);

// execute the script and await the result.
var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);

// print the resulting pipeline objects to the console.
var output = new StringBuilder();
foreach (var item in pipelineObjects)
{
output.Append(item.BaseObject.ToString());
}

return output.ToString();
}

知道可能是什么问题吗?

最佳答案

通过创建自定义运行空间解决了这个问题。引用这篇文章-How to run powershell core scripts from net core applications

关于powershell - 在 .NetCore 应用程序中运行 PS 时出错 - 无法执行操作,因为运行空间不在 'Opened' 状态。运行空间的当前状态是 'Broken',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64671333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com