【问题标题】:Running Powershell Workflow from C#从 C# 运行 Powershell 工作流
【发布时间】:2015-05-19 10:40:36
【问题描述】:

我正在尝试运行此代码:

    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 PowerShell 不支持 Windows PowerShell 工作流 基于 x86 的控制台。打开基于 Windows PowerShell x64 的控制台,然后 然后再试一次。

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

【问题讨论】:

  • 是否可以选择将程序集的目标平台更改为 x86?
  • 不,不幸的是
  • 您为什么要问与问题标题不同的问题?请考虑将您的问题重命名为“如何从 C# 打开基于 x64 的 Powershell 实例?”

标签: c# powershell workflow powershell-workflow


【解决方案1】:

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

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

【讨论】:

  • System.Management.Automation.RunspaceMode 对我来说不存在。我能看到的最接近的是 System.Management.Automation.RunspaceInvoke
  • @Backwards_Dave 哦,我在下面看到了你的帖子。那么参考存储在 %PROGRAMFILES% 中的 x64 程序集怎么样? (哪个程序集是 x64)对不起,我不能给你一个好的解决方案:O
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2019-09-30
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多