C#中运行PowerShell需要用到System.Management.Automation.dll。在Visual Studio中可以通过NuGet添加引用,package名字为“System.Management.Automation”。

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
using (Pipeline pipeline = runspace.CreatePipeline())
 {
     pipeline.Commands.AddScript("Powershell Script"");
     // Get result
    Collection<PSObject> results = pipeline.Invoke();     
 }
runspace.Close();

在64位系统上调试时,可能需要将Build平台改为x64,否则系统调用的Powershell为32位的。

相关文章:

  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-05-01
  • 2022-01-08
  • 2021-07-12
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2021-08-05
  • 2022-03-06
  • 2021-04-09
  • 2021-06-05
  • 2021-06-12
相关资源
相似解决方案