OSD Scripting: How to kill a process using VB Script
Hi Friends,
I'm a sequencing a application, where-in I need to kill a process just before the shutdown of my shortcut. I know PRE SHUTDOWN is not supported. So i have scripted to kill a process POST LAUNCH i.e.
<DEPENDENCY>
<SCRIPT EVENT="LAUNCH" PROTECT="TRUE" TIMING="POST" WAIT="TRUE">
<SCRIPTBODY>"C:\apps\oracle\client_1020_eng\Kill.vbs"</SCRIPTBODY>
</SCRIPT>
</DEPENDENCY>
And VBS is
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'launch.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
msgbox "Prcoess Killed"
But somehow the process is not killed on launching shortcut.
Please someone guide where I'm doing wrong.
I'm a sequencing a application, where-in I need to kill a process just before the shutdown of my shortcut. I know PRE SHUTDOWN is not supported. So i have scripted to kill a process POST LAUNCH i.e.
<DEPENDENCY>
<SCRIPT EVENT="LAUNCH" PROTECT="TRUE" TIMING="POST" WAIT="TRUE">
<SCRIPTBODY>"C:\apps\oracle\client_1020_eng\Kill.vbs"</SCRIPTBODY>
</SCRIPT>
</DEPENDENCY>
And VBS is
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'launch.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
msgbox "Prcoess Killed"
But somehow the process is not killed on launching shortcut.
Please someone guide where I'm doing wrong.
0 Comments
[ + ] Show comments
Answers (5)
Please log in to answer
Posted by:
lurulu
15 years ago
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill1, strProcessKill2
strComputer = "."
strProcessKill1 = "'iexplore.exe'"
strProcessKill2 = "'firefox.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill1 )
For Each objProcess in colProcess
objProcess.Terminate()
Next
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill2 )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WScript.Quit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill1, strProcessKill2
strComputer = "."
strProcessKill1 = "'iexplore.exe'"
strProcessKill2 = "'firefox.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill1 )
For Each objProcess in colProcess
objProcess.Terminate()
Next
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill2 )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WScript.Quit
Posted by:
TheGuru
14 years ago
Simple dos command - Taskkill /blah blah
http://www.computerhope.com/taskkill.htm
WShell.Run "Taskkill /blah blah" in your VBscript and done
http://www.computerhope.com/taskkill.htm
WShell.Run "Taskkill /blah blah" in your VBscript and done
Posted by:
Jsaylor
14 years ago
Some thoughts:
Your script should execute and close "launch.exe" just fine, but have you tried using it outside of the task sequencer to see if it will successfully close the process, just to make sure?
Some programs use more than one executable, are there others that should be closed as well?
You'll want to remove the msgbox command at the end of that script, you shouldn't require any user input from the system account.
Good luck!
Your script should execute and close "launch.exe" just fine, but have you tried using it outside of the task sequencer to see if it will successfully close the process, just to make sure?
Some programs use more than one executable, are there others that should be closed as well?
You'll want to remove the msgbox command at the end of that script, you shouldn't require any user input from the system account.
Good luck!
Posted by:
anonymous_9363
14 years ago
...and, like practically every script I see, there is ZERO error-trapping. After executing the Terminate method, how do you know that the process actually terminated? How do you even know if the WMI object got created? You don't, for either - the script just assumes that everything works. BIG mistake...
Posted by:
TheGuru
14 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.