且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过C#code PowerShell命令

更新时间:2023-02-11 10:31:18

根据这篇文章的如何从C#运行PowerShell脚本,你需要的东西是这样的:

According to this article How to run PowerShell scripts from C#, you will need something like this:

// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(String.Format("$user = \"{0}\"", userName));
pipeline.Commands.AddScript("#your main script");

// execute the script
Collection<psobject> results = pipeline.Invoke();
// close the runspace
runspace.Close();

另请参见Run PowerShell的脚本从#1 C#应用程序的问题在这里。