Ok, we've all done it. You write a script, you think it's the bomb....you click run and realize...oops. Whatever the reason, you want to kill that little sucker and if you're unlucky enough to be running a script that moves the mouse and sends key strokes then scrambling to open taskmanager and find wscript.exe is more of a pain than it's worth.
With a little forward planning you can be prepared. Just make a copy of this script and keep a shortcut to it handy (I keep one in the quick launch bar for one-click-emergencies).
'-----------------------------------------------------------
Option Explicit
Dim objWMI, objProc, colProc
Dim strComputer, strProc
strComputer = "."
strProc = "'wscript.exe'"
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProc = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = " & strProc )
For Each objProc in colProc
objProc.Terminate()
Next
'-----------------------------------------------------------
Of course this same script could be modified to terminate any running process easily enough. The only real caveat is that whatever value you pass to strProc would need to be encapsulated in single quotes, or you would need to change the ExecQuery string to include those automatically. Ah heck, here you go.
function killProc(strProc)
Dim objWMI, objProc, colProc, strComputer
strProc = "'" & strProc & "'"
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProc = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = " & strProc )
For Each objProc in colProc
objProc.Terminate()
Next
end function
Enjoy,
No comments:
Post a Comment