简体   繁体   English

从C#运行Powershell工作流

[英]Running Powershell Workflow from C#

I'm trying to run this code: 我正在尝试运行以下代码:

    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);
    }

But I get this error: 但是我得到这个错误:

Windows PowerShell Workflow is not supported in a Windows PowerShe ll x86-based console. Windows PowerShell基于x86的控制台不支持Windows PowerShell工作流。 Open a Windows PowerShell x64-based console, and then try again. 打开一个Windows PowerShell基于x64的控制台,然后再试一次。

How can I open a x64 based instance of Powershell from C#? 如何从C#打开基于x64的Powershell实例?

how about this? 这个怎么样? according to MSDN, PowerShell.Create(RunspaceMode) method introduced in 'Windows PowerShell 3.0'. 根据MSDN,“ Windows PowerShell 3.0”中引入了PowerShell.Create(RunspaceMode)方法。 hope this will works. 希望这会起作用。

using (PowerShell PowerShellInstance =  PowerShell.Create(RunspaceMode.CurrentRunspace)) {
}

Here is a work around that doesn't involve trying to use RunspaceMode.CurrentRunspace (which is hard to use because getting the dll to work is tricky ) 这是一种解决方法,它不涉及尝试使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM