VBScript: Betcha never thought of this one
I mentioned in a previous post that I had to solve a little issue with the wonderful WMCMD.VBS script that Alex Zambelli maintains. Because of some occasional nastyness, when the script is done–successful or not– it tries to kill it’s own process, in case any of the WM Encoder objects gets hung up.
The original script uses WMI to look on the machine for the cscript process for the currently running script:
| function TerminateEncoderProcess()
dim objWMIService On Error Resume Next ‘ Get Windows Manager object ‘ Enumerate all CScript.exe processes ‘ Kill the processes that match this one in name and arguments end function |
Unfortunatly, this script, when all else fails, tries to kill itself by finding the first running cscript, and killing it. Hmmm. not too good, I had multiple encoder script processes going on, and it kept killing the wrong one.
A pity it seems so hard to find the current process in VBScript… until I thought about it a bit more:
| function TerminateEncoderProcess()
GetObject("winmgmts:root\cimv2:Win32_Process.Handle=’" _ end function |
Huh?
This version of the script spawns off a new cmd.exe process (which exits nearly instantly), but uses the process ID from that, looks up the process, and get it’s parent process, and then terminate that. Nice thing is, it don’t get confused
