使用特殊名称启动进程,或使用句柄来终止它

Dr.*_*YSG 3 powershell powershell-3.0

我们有许多node.js程序,都是用PowerShell创建的.我们使用start-process在后台生成node.js可执行文件(node.exe).

我们想给每个人一个不同的"句柄",以便我们以后可以使用powershell命令stop-process来杀死它们.例如,start-process -Name'wServer'要删除的powershell脚本将不同于要启动的脚本,并且不同的用户可能会使用它.

我坚持如何识别每个node.exe不同.它不是可执行文件,而是app.js的路径

以下是我们如何开始其中之一:

$reg = Get-Item -Path "hklm:\SOFTWARE\us\EGPL\GeoLibrarian" 
$path = $reg.GetValue('LibrarianPath').ToString() 
$sixtyfour = [Environment]::Is64BitProcess

# Now start running Watson
$node = "C:\Program Files\nodejs\node.exe" 
$arg = "app.js"
$dir = "$path\Watson"
start-process -WorkingDirectory $dir $node $arg
Write-Host "64-Bit Powershell: "$sixtyfour
Write-Host "PowerShell Version: "$PSVersionTable.PSVersion
Write-Host "Watson running in background"
Run Code Online (Sandbox Code Playgroud)

现在,我可以通过这个序列杀死以独特窗口开始的那些,我不认为在powershell中启动的窗口会有一个窗口.

Write-Host "Kill watson task"
$watson = get-process | where-object {$_.MainWindowTitle -eq 'WatsonJS'}
Stop-Process -Id $watson.Id -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)

Rya*_*ose 5

一种方法是使用该-PassThru参数,该参数导致Start-Process返回可用于控制过程的对象.完成该过程后,将对象传递给Stop-Process(或调用对象的Kill()方法)

如果需要跨PS会话存储对象,可以使用将变量保存到XML文件Export-Clixml.之后,使用补充水分变量Import-Clixml.

PS会议#1
$proc = Start-Process notepad -Passthru
$proc | Export-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
Run Code Online (Sandbox Code Playgroud) PS会议#2
$proc = Import-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
$proc | Stop-Process
Run Code Online (Sandbox Code Playgroud)