PowerShell:启动进程在子进程运行时从不退出
我使用启动流程启动流程,该流程也启动流程:PowerShell:启动进程在子进程运行时从不退出,powershell,Powershell,我使用启动流程启动流程,该流程也启动流程: $p = Start-Process "ProcessRunner.exe" -ArgumentList "notepad.exe" -NoNewWindow -Wait -PassThru $exitCode = $p.ExitCode ProcessRunner(父进程)启动在参数列表中指定的应用程序('notepad.exe'-子进程),然后使用脚本另一部分中使用的特定exitCode退出 在powershell 2.0中,这和我预期的一样:
$p = Start-Process "ProcessRunner.exe" -ArgumentList "notepad.exe" -NoNewWindow -Wait -PassThru
$exitCode = $p.ExitCode
ProcessRunner(父进程)启动在参数列表中指定的应用程序('notepad.exe'-子进程),然后使用脚本另一部分中使用的特定exitCode退出
在powershell 2.0中,这和我预期的一样:ProcessRunner退出后脚本立即继续,即使子进程继续执行
在powershell 4.0中,脚本执行将暂停,直到notepad.exe也退出
当子进程仍在运行时(即ps2的行为),是否有可能(在ps4中)获取父进程的exitCode
编辑:
可用于重现问题的简单ProcessRunner:
static int Main(string[] args)
{
int result;
try
{
Process prc = new Process();
prc.StartInfo.FileName = args[0];
result = prc.Start() ? 0 : 1;
}
catch (Exception e)
{
result = -1;
}
Console.WriteLine("ExitCode = {0}", result);
return result;
}
使用
$p.WaitForExit()
而不是-Wait
会带来更好的结果(脚本会根据请求继续执行),但是$p.ExitCode
没有设置,它的值是$null
,因为您可以通过编写主机“[PS]ExitCode=$p.ExitCode”
(打印结果是[PS]ExitCode=
)尝试删除-等待?此外,为了能够适应,我觉得我们至少需要了解一些ProcessRunner
,但我不确定这是一个合法的实用程序,还是您自己构建的?请参阅编辑的问题(添加了ProcessRunner示例,-等待部分帮助)