alexander.teno
alexander.teno

Reputation: 129

Invoking a x86 PowerShell Script from an x64 C# Application

I've looked through several Questions and didn't find anything that was similar enough to apply to my situation (from what I could tell).

I have a x64 Application (I am not able to change architecture as per design requirements) and it needs to Invoke a PowerShell Script under the x86 Architecture.

var runspaceConfiguration = RunspaceConfiguration.Create();    
var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

var pipeline = runspace.CreatePipeline();

var myCommand = new Command(@"MY-COMMAND");
myCommand.Parameters.Add("Path", @"C:\");

pipeline.Commands.Add(myCommand);

return pipeline.Invoke();

If anyone can give me an idea how I can start a x86-PowerShell session from C# I would greatly appreciate it.

Edit: I'll update this with corrected code once I've ironed out the details.

Upvotes: 5

Views: 2438

Answers (1)

Keith Hill
Keith Hill

Reputation: 201642

Run script in your x64 C# app that starts a job to execute the 32-bit script. Be sure to use the -RunAs32 switch on Start-Job. This will require PowerShell 2.0 or higher.

pipeline.Commands.AddScript("Start-Job -Scriptblock { My-Command.exe -Path C:\ } -Name MyCommandJob -RunAs32");

You will need to retrieve the results using Receive-Job -Name MyCommandJob.

Upvotes: 7

Related Questions