Home Orchestrator

Handy tip for getting long running .net powershell scripts in scorch to stop

Jerry_VeldhuisJerry_Veldhuis Customer Advanced IT Monkey ✭✭✭
edited February 2017 in Orchestrator
In case someone else runs into this issue, I thought I'd share.

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);
}
Now the admin can stop the runbook, then simply monitor for c:\temp\status.log to update. Copy and modify to fit your environment and need.
Sign In or Register to comment.