启动进程并仅等待父进程 [英] Start-Process And Wait For Parent Process Only

查看:74
本文介绍了启动进程并仅等待父进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PowerShell 语法:

I'm using the PowerShell syntax:

Start-Process -FilePath $file -ArgumentList $argList -NoNewWindow -Wait

正在启动的进程是一个安装程序,它安装 Windows 服务,然后启动该服务,然后进程停止.发生的事情是我的脚本无限期挂起,即使在进程成功完成并且进程已经退出之后(正如我在任务管理器中看到的那样.当我从命令行运行它时,没有 PowerShell,它在一分钟内完成).显然,它仍在等待,因为有一个从原始进程产生的子进程.如何告诉 PowerShell 只等待初始进程($file)而不等待任何子进程?

The process being started is an installer, which installs a Windows service and then starts up that service, then the process stops. What's happening is that my script is hanging indefinitely, even after the process completed successfully and the process has exited (as I can see in Task Manager. When I run it from a command line, without PowerShell, it's done in under a minute). Apparently, it's still waiting because there's a child process which was spawned from the original process. How can I tell PowerShell to wait only for the initial process ($file) and not any children?

谢谢

推荐答案

最后,我决定简单地用 .NETy 方式来做,尽管我更喜欢用 Powershelly 方式来做.以下代码复制自 https://stackoverflow.com/a/8762068/1378356:

In the end, I decided to simply do it the .NETy way, even though I had preferred a Powershelly way of doing it. Code below is copied from https://stackoverflow.com/a/8762068/1378356:

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $file
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $argList
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()    
Write-Host "exit code: " + $p.ExitCode

这种方式没有问题.

这篇关于启动进程并仅等待父进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆