We have a number of node.js programes that all are created with powershell. We used the start-process to spawn the node.js executable in the background (node.exe).
We would like to give each one a different "handle" so that we can later use the powershell command stop-process to kill them. e.g. start-process -Name 'wServer' The powershell script to kill, will be different than the one to start, and a different user might well use it.
I am stuck on how to identify each node.exe different. It is not the executable that is different but the path to the app.js
Here is how we start one of them:
$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"
right now, I can kill ones started with a unique window via this sequence, I don't think the ones started in powershell will have a window.
Write-Host "Kill watson task"
$watson = get-process | where-object {$_.MainWindowTitle -eq 'WatsonJS'}
Stop-Process -Id $watson.Id -ErrorAction SilentlyContinue
One method is to use the -PassThru
parameter, which causes Start-Process
to return an object which can be used to control the process. When you're done with the process, pipe the object to Stop-Process
(or call the object's Kill()
method)
In cases where you need to store the object across PS sessions, you can save the variable to an XML file using Export-Clixml
. Later, rehydrate the variable using Import-Clixml
.
$proc = Start-Process notepad -Passthru
$proc | Export-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
PS Session #2
$proc = Import-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
$proc | Stop-Process
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With