Handy tip for getting long running .net powershell scripts in scorch to stop
I have a runbook that runs for about and hour, processing data and I ran into an issue where the .net script in scorch would be stopped during maintenance windows and the powershell script, running in a subshell wouldn't stop. Even if the admin could tell the runbook was currently active, trying to locate and kill the "right" powershell process using taskmgr or powershell scripting was painful.
Anyway, the trick is in the child powershell module, at regular intervals (in my case between processing some amount of work), call Get-WMIObject to monitor the health of your parent process. If it disappears, it means the scorch runbook has been stopped.
$myParentID=(Get-WmiObject win32_process -filter "ProcessID=$PID").ParentProcessID if ( (Get-WmiObject win32_process -filter "ProcessID=$myParentID") -eq $null ) { "no parent process, stopping" | out-file -FilePath c:\temp\status.log exit(0); }