Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring a process to see when it stops

Status
Not open for further replies.

mshicks

Technical User
Mar 5, 2011
3
US
I am writing a script to uninstall a program from a large number of workstations. I have every part of the script working except one.

Instead of just having the script sleep for an estimated time during the uninstall before it restarts, I am trying to poll the process to see when it quits.

I am doing this by using the winsmgmt object. I am reading all the processes into an array then filtering that array for the process I'm looking for. Then I test for the existence of the process if it finds it the script sleeps for a minute, clears the array, loops and starts over.

Everything works great until the process I am monitoring stops. Then the script doesn't exit the loop. I know the filter is working because it stops sending me the name of the process through the echo command. I know I am probably missing something simple here or there is a lot better way to do this than what I am trying to do. If anybody has any ideas please let me know.

Here is my code.




IsZenAlive = 1
Do While IsZenAlive = 1
i=0
For each Process in Service.InstancesOf ("Win32_Process")
i=i+1
Next
ReDim arrProcesses(i)
i=0
For each Process in Service.InstancesOf_ ("Win32_Process")
arrProcesses(i)=Process.Name
i=i+1
Next
arrZenProcess=Filter(arrProcesses,"zenworksuninstall",True,1)
UninstRunning=Join(arrZenProcess)
WScript.Echo UninstRunning
If UninstRunning <> "zenworksuninstall" Then
WScript.Sleep(60000)
Erase arrProcesses
IsZenAlive = 1
Else
IsZenAlive = 0
End If
Loop
 
It's likely that [tt] If UninstRunning <> "senworksuninstall" Then[/tt] never evaluates to false; probably because the process name is misspelled or is in the incorrect case.

Simply wrap [tt]UninstRunning[/tt] in an lcase() so it converts the process name to lowercase - the case in which the comparison occurs.


Personally, I would not gather all processes and monitor just one. I would monitor the one process and constantly check if it exists.

Code:
do
    wscript.sleep 60000 [green]'1 minute[/green]
    set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where Name='process_name.exe'")
loop until (colProcesses.count = 0)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top