C# 从C运行Powershell工作流#

C# 从C运行Powershell工作流#,c#,powershell,workflow,powershell-workflow,C#,Powershell,Workflow,Powershell Workflow,我正在尝试运行以下代码: using (PowerShell PowerShellInstance = PowerShell.Create()) { _outputCollection.DataAdded += outputCollection_DataAdded; PowerShellInstance.AddScript(@"workflow test { Write-Warning 'this is a warning'; }; test");

我正在尝试运行以下代码:

    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);
    }
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
_outputCollection.DataAdded+=outputCollection\u DataAdded;
AddScript(@“工作流测试{Write Warning'这是一个警告';};测试”);
IAsyncResult result=PowerShellInstance.BeginInvoke(null,_outputCollection);
}
但我得到了这个错误:

Windows PowerShell中不支持Windows PowerShell工作流 基于x86的控制台。打开基于Windows PowerShell x64的控制台,然后 然后再试一次


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

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

使用(PowerShell PowerShellInstance=PowerShell.Create(RunspaceMode.CurrentRunspace)){
}

这里有一个解决方法,它不涉及尝试使用
运行空间模式。CurrentRunspace
(这很难使用,因为要让dll正常工作非常困难)

WSManConnectionInfo connectionInfo=newwsmanconnectioninfo();
//创建并打开运行空间。
Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo);
Open();
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
_outputCollection.DataAdded+=outputCollection\u DataAdded;
AddScript(@“工作流测试{Write Warning'这是一个警告';};测试”);
IAsyncResult result=PowerShellInstance.BeginInvoke(null,_outputCollection);
}
runspace.Close();

将程序集的目标平台更改为x86是一种选择吗?不,这不是不幸的。你为什么要问与问题标题不同的问题?请考虑将您的问题重命名为“我如何才能打开一个基于C 64的PuxBeice的实例?”System.Management.Automation.RunspaceMode对我来说是不存在的。我能看到的最接近的是System.Management.Automation。RunspaceInvoke@Backwards_Dave哦,我在下面看到了你的帖子。那么存储在%PROGRAMFILES%中的引用x64程序集如何?(哪个程序集是x64)对不起,我不能给你一个好的解决方案:O
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();