运行独立于C#应用程序的Powershell脚本

问题描述:

代码的目标是:将秒的整数值传入Powershell脚本,然后用于告诉Powershell脚本等待,然后重新启动控制台应用程序。运行独立于C#应用程序的Powershell脚本

问题:Powershell脚本实例存在于应用程序内部。这意味着当我想退出应用程序并保持脚本运行时,应用程序一直等到脚本完成。

PowerShell psExec = PowerShell.Create(); 
psExec.AddCommand(@powershellScriptLocation); 
psExec.AddArgument(Convert.ToInt32(CalculateTimeToWait().TotalSeconds)); 
psExec.Invoke(); 
+1

你想安排一个工作为或使用PowerShell的后台工作 – 4c74356b41

+0

你可以简单地尝试使用'的Process.Start()'和使用'PowerShell.exe'启动脚本。 –

你可能需要这样的事:)

var psi = new ProcessStartInfo("powershell.exe", "pathToYourScript and args"); 
var process = Process.Start(psi); 
process.WaitForExit();