I'd like to creating a small vbs file to kill a short list of processes/tasks and services
before installing other software.

There are a lot of third party utilities (ie. exes) that do this, but I hoping I could do
it without calling a batch/command-prompt process and do it all in a single file.

Any feedback is appreciated.

Thanks,

--
Tony Pedretti
TransUnion LLC

Re: Kill processes and services by Torgeir

Torgeir
Wed Sep 08 15:36:32 CDT 2004

Tony Pedretti wrote:

> I'd like to creating a small vbs file to kill a short list of processes/tasks and services
> before installing other software.
>
> There are a lot of third party utilities (ie. exes) that do this, but I hoping I could do
> it without calling a batch/command-prompt process and do it all in a single file.
>
> Any feedback is appreciated.
Hi

Using WMI to terminate a process:
http://groups.google.com/groups?selm=%235mn00BcEHA.368%40TK2MSFTNGP10.phx.gbl


To stop a service, you can use WMI or ADSI's WinNT provider:
http://groups.google.com/groups?selm=3F4947CC.7CC40904%40hydro.com


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Kill processes and services by Tony

Tony
Thu Sep 09 11:10:33 CDT 2004

Torgeir,

Thanks for the references.
I've taken them and with other sources produced the following script.

One last improvement I'd like to make is to have the array loop be dynamic.
That is I've found I can't increment the Do Until against the array (aryProcess) until its
value is NULL or "". The scripts just errors since position (6) doesn't exist to test
against.

I'd like to have the test condition read something to the effect of...
Do Until aryProcess(intArrayNumber) = NULL

Anyone have any suggestions?


Const ADS_SERVICE_STOPPED = 1
Dim aryProcess, intArrayNumber, strComputer, WshShell, objComputer, objService,
objWMIservice, colProcessList

aryProcess = Array("aaaaaaaa.exe", "bbbbbbb.exe", "cccccccc.exe", "xxxxx.exe",
"yyyyyy.exe", "zzzzzzz.exe")
intArrayNumber = 0
strComputer = "."

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")

On Error Resume Next

Set objService = objComputer.GetObject("Service", "xxxxx")
If Err.Number = 0 Then
If objService.Status <> ADS_SERVICE_STOPPED Then
objService.Stop
End If
End If

Err.Clear

Set objService = objComputer.GetObject("Service", "yyyyy")
If Err.Number = 0 Then
If objService.Status <> ADS_SERVICE_STOPPED Then
objService.Stop
End If
End If

On Error Goto 0


Set objWMIservice = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")

Do Until intArrayNumber = 6
Set colProcessList = objWMIservice.ExecQuery ("SELECT * from Win32_Process WHERE Name =
'" & aryProcess(intArrayNumber) & "'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

intArrayNumber = intArrayNumber + 1
Loop


--
Tony Pedretti
TransUnion LLC



Re: Kill processes and services by Torgeir

Torgeir
Thu Sep 09 16:33:50 CDT 2004

Tony Pedretti wrote:

> Torgeir,
>
> Thanks for the references.
> I've taken them and with other sources produced the following script.
>
> One last improvement I'd like to make is to have the array loop be dynamic.
> That is I've found I can't increment the Do Until against the array (aryProcess) until its
> value is NULL or "". The scripts just errors since position (6) doesn't exist to test
> against.
>
> I'd like to have the test condition read something to the effect of...
> Do Until aryProcess(intArrayNumber) = NULL
>
> Anyone have any suggestions?
Hi

To get the number of elements in an array (note that array
counts start at 0):

'--------------------8<----------------------
aryProcess = Array("aaaaaaaa.exe", "bbbbbbb.exe", "cccccccc.exe", _
"xxxxx.exe", "yyyyyy.exe", "zzzzzzz.exe")

WScript.Echo UBound(aryProcess)

'--------------------8<----------------------


But it is much easier to use this:

For Each strProcess In aryProcess
WScript.Echo strProcess
Next

like here:

'--------------------8<----------------------
aryProcess = Array("aaaaaaaa.exe", "bbbbbbb.exe", "cccccccc.exe", _
"xxxxx.exe", "yyyyyy.exe", "zzzzzzz.exe")

'(snip other code)

strComputer = "."
Set objWMIservice = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

For Each strProcess In aryProcess
Set colProcessList = objWMIservice.ExecQuery _
("SELECT * from Win32_Process WHERE Name = '" & strProcess & "'")
For Each objProcess in colProcessList
objProcess.Terminate()

Next
Next
'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Kill processes and services by Tony

Tony
Fri Sep 10 09:33:11 CDT 2004

Torgeir,

The answer was right in front of me the whole time.
Put in For/Next loops in for both the process and service routines. Works great.

Thanks again,

Tony