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#?

3
  • 1
    Is changing the target platform of your assembly to x86 an option? Commented May 19, 2015 at 0:04
  • no it's not unfortunately Commented May 19, 2015 at 23:38
  • Why are you asking something different than the title of your question? Please consider renaming your question to "How can I open a x64 based instance of Powershell from C#?" Commented Jun 5, 2015 at 16:13

2 Answers 2

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)) {
}
Sign up to request clarification or add additional context in comments.

2 Comments

System.Management.Automation.RunspaceMode doesn't exist for me. The closest I can see is System.Management.Automation.RunspaceInvoke
@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
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.