我使用 Start-Process 来启动进程,该进程也启动进程:
$p = Start-Process "ProcessRunner.exe" -ArgumentList "notepad.exe" -NoNewWindow -Wait -PassThru
$exitCode = $p.ExitCode
Run Code Online (Sandbox Code Playgroud)
ProcessRunner(父进程)启动ArgumentList(“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;
}
Run Code Online (Sandbox Code Playgroud)
使用$p.WaitForExit()
代替会-Wait
带来更好的结果(脚本继续执行 - 按要求)但未$p.ExitCode
设置,其值是您可以通过(打印结果是)$null
自行确定的Write-Host "[PS] ExitCode = $p.ExitCode"
[PS] ExitCode =
归档时间: |
|
查看次数: |
1402 次 |
最近记录: |