简体   繁体   English

从x64 C#应用程序调用x86 PowerShell脚本

[英]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.

相关问题 如何从 c# 中的 x86 应用程序启动 x64 位应用程序 - How to start x64 bit application from x86 application in c# C# 中的 x86/x64 CPUID - x86/x64 CPUID in C# C# 支持 x64 和 x86 - C# support both x64 and x86 如何使用C#在x64或x86中运行PowerShell? - How to run PowerShell in x64 or x86 using C#? 用于C#应用程序的AnyCPU / x86 / x64及其C ++ / CLI依赖性 - AnyCPU/x86/x64 for C# application and it's C++/CLI dependency 在项目设置为x86时,在C#中启动x64 Windows应用程序 - Launch x64 Windows application in C# while the project is set to x86 X64 应用程序比 X86 慢 - X64 Application is slow compared to X86 VisualStudio C#x64,为什么AddReference选项,.NET选项卡指向x86 DLL而不是x64? - VisualStudio C# x64, why AddReference option, .NET tab points to x86 DLL instead of x64? c#visual studio使用目标anycpu构建exe,但在调用进程平台(x86 / x64)上确定其平台(x86 / x64) - c# visual studio build exe with target anycpu but that determines its platform(x86/x64) on the calling process platform(x86/x64) 结合C#和C ++项目适用于x86和x64,但不适用于ARM - Combining a C# and C++ project works for x86 and x64 but not for ARM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM