我能从Start-Process,而不是CMD过程得到实际的咕噜声吗?
我在用着
Start-Process grunt -ArgumentList 'serve' -PassThru
并将其添加到ArrayList
。 当我查看所说的ArrayList
它显示了cmd
进程,而不是实际启动的node.js
进程。 如果我尝试Stop-Process
它,很显然,杀死cmd
进程,而不是运行的grunt
。 我可以用Get-Process node
find正在运行的grunt
。
问题是我需要同时运行多个咕噜声,我想要一种方式来区分它们。 有什么办法可以让我的实际node
进程到PowerShell初始化?
发生这种情况是因为grunt
是通过CMD
运行的grunt.cmd
启动的。 该文件在node
启动grunt
。
这里是一个如何find从CMD启动notepad2.exe类似于你的例子的例子:
# start a CMD which starts notepad2 and then waits on it $process = Start-Process -PassThru 'cmd.exe' '/c "C:\Program Files\Notepad2\Notepad2.exe"' # Wait for notepad2 to be launched Start-Sleep -Seconds 2 # find the children of CMD, and kill the one which is notepad2 (Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'Notepad2.exe'") | %{ Stop-Process -Id $_.ProcessId}
翻译这个咕噜声:
# start Grunt via grunt.cmd $process = Start-Process grunt -ArgumentList 'serve' -PassThru # Wait for node to be launched Start-Sleep -Seconds 2 # find the children of CMD, and kill the one which is node (Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'node.exe'") | %{ Stop-Process -Id $_.ProcessId}
grunt
不是Win32可执行文件,它是由node.exe
运行的JavaScript文件。 获取运行咕噜声的节点的实例最简单的方法是自己启动它:
$ps = start-process -passthru node -argumentlist "$env:APPDATA\npm\node_modules\grunt\bin\grunt"