我正在尝试启动一个Runspace实例来激发一些Powershell命令。但是如果我使用"runspace.Open()“方法,程序就会卡住。一段时间后,该方法在错误消息中失败:
System.Management.Automation.Remoting.PSRemotingTransportException HResult=0x80131501
Nachricht = The background process reported an error with the following message: D
Quelle = System.Management.Automation它们是对应的代码行:
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控制台应用程序和以下软件包:
如果我在.NET框架控制台应用程序中运行相同的代码,那么一切都正常。有解决这个问题的建议吗?
发布于 2021-09-03 13:16:10
解决方案是使用PowerShellProcessInstance的参数化构造函数,因此,在服务器模式下启动Powershell:
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();https://stackoverflow.com/questions/69000069
复制相似问题