首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET Core5.0 Runspace.Open()不工作

.NET Core5.0 Runspace.Open()不工作
EN

Stack Overflow用户
提问于 2021-08-31 13:57:19
回答 1查看 354关注 0票数 1

我正在尝试启动一个Runspace实例来激发一些Powershell命令。但是如果我使用"runspace.Open()“方法,程序就会卡住。一段时间后,该方法在错误消息中失败:

代码语言:javascript
复制
System.Management.Automation.Remoting.PSRemotingTransportException HResult=0x80131501 
Nachricht = The background process reported an error with the following message: D 
Quelle = System.Management.Automation

它们是对应的代码行:

代码语言:javascript
复制
Runspace rs;
var pspi = new PowerShellProcessInstance();
pspi.Process.StartInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
pspi.Process.Start();
rs = RunspaceFactory.CreateOutOfProcessRunspace(new TypeTable(new string[0]), pspi);
rs.Open();

using (PowerShell powershell = PowerShell.Create()) {
    powershell.Runspace = rs;
    powershell.AddScript(<some code>);
    var output = powershell.Invoke();
}

我使用的是一个.NET核心5控制台应用程序和以下软件包:

  • Microsoft.PowerShell.SDK
  • System.Management.Automation

如果我在.NET框架控制台应用程序中运行相同的代码,那么一切都正常。有解决这个问题的建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-03 13:16:10

解决方案是使用PowerShellProcessInstance的参数化构造函数,因此,在服务器模式下启动Powershell:

代码语言:javascript
复制
Version psVersion = new Version("5.1");
TypeTable type = new TypeTable(new string[0]);

powershellInstance = new PowerShellProcessInstance(psVersion, null, null, false);
powershellInstance.Process.StartInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";

runspace = RunspaceFactory.CreateOutOfProcessRunspace(type, powershellInstance);
runspace.Open();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69000069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档