[英]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. 我有一个x64应用程序(我无法根据设计要求更改架构),它需要在x86架构下调用PowerShell脚本。
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. 如果有人能告诉我如何从C#开始x86-PowerShell会话,我将非常感激。
Edit: I'll update this with corrected code once I've ironed out the details. 编辑:一旦我解决了细节,我会用更正的代码更新这个。
Run script in your x64 C# app that starts a job to execute the 32-bit script. 在x64 C#app中运行脚本,启动作业以执行32位脚本。 Be sure to use the
-RunAs32
switch on Start-Job
. 务必在
Start-Job
上使用-RunAs32
开关。 This will require PowerShell 2.0 or higher. 这将需要PowerShell 2.0或更高版本。
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
. 您需要使用
Receive-Job -Name MyCommandJob
检索结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.