1

我正在尝试运行此代码:

    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        _outputCollection.DataAdded += outputCollection_DataAdded;
        PowerShellInstance.AddScript(@"workflow test { Write-Warning 'this is a warning'; }; test");
        IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, _outputCollection);
    }

但我得到这个错误:

基于 Windows PowerShe ll x86 的控制台不支持 Windows PowerShell 工作流。打开基于 Windows PowerShell x64 的控制台,然后重试。

如何从 C# 打开基于 x64 的 Powershell 实例?

4

2 回答 2

2

这个怎么样?根据 MSDN,“Windows PowerShell 3.0”中引入的 PowerShell.Create(RunspaceMode) 方法。希望这会奏效。

using (PowerShell PowerShellInstance =  PowerShell.Create(RunspaceMode.CurrentRunspace)) {
}
于 2015-05-19T00:08:16.133 回答
0

这是一个不涉及尝试使用的解决方法RunspaceMode.CurrentRunspace(这很难使用,因为让 dll 工作很棘手

WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
//Create and open a runspace.
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    _outputCollection.DataAdded += outputCollection_DataAdded;
     PowerShellInstance.AddScript(@"workflow test { Write-Warning 'this is a warning'; }; test");
     IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, _outputCollection);
}
runspace.Close();
于 2015-05-19T02:40:36.597 回答