我尝试从 c# 执行 PowerShell 脚本而不等待结果。
string cmdArg = "MyScript.ps1";
//Create New Runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
//Adds the command
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
//Do not wait for a result
pipeline.InvokeAsync();
runspace.Close();
但它要等到脚本完成!