1

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. Open a Windows PowerShell x64-based console, and then try again.

How can I open a x64 based instance of Powershell from C#?

David Klempfner
  • 8,700
  • 20
  • 73
  • 153

2 Answers2

2

how about this? according to MSDN, PowerShell.Create(RunspaceMode) method introduced in 'Windows PowerShell 3.0'. hope this will works.

using (PowerShell PowerShellInstance =  PowerShell.Create(RunspaceMode.CurrentRunspace)) {
}
SlaneR
  • 684
  • 5
  • 19
  • System.Management.Automation.RunspaceMode doesn't exist for me. The closest I can see is System.Management.Automation.RunspaceInvoke – David Klempfner May 19 '15 at 00:21
  • @Backwards_Dave oh I saw your post below. then how about reference x64 assembly that stored in %PROGRAMFILES%? (which assembly is x64) sorry I can't give you a good solution :O – SlaneR May 20 '15 at 13:39
0

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)

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();
Community
  • 1
  • 1
David Klempfner
  • 8,700
  • 20
  • 73
  • 153