View Single Post
Old Jul 12th, 2006, 5:22 PM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 318
Rep Power: 4 mackenga is on a distinguished road
I wish I'd seen this post sooner; I've just written something similar at work! I have a small suggestion which would improve the efficiency of this script a little; see the section that reads:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)

'Going to cycle through all process running and incrementing. 
'If notepad is found in running process, it'll updated "NoteRun" var w/ a numeric
'Put a 0 in the NoteRun var
		NoteRun = 0	
For Each objItem in colItems
			
		'Checks to see if notepad is in the list of running processes.
		if objItem.Caption = "notepad.exe" then
			NoteRun = 1
		else 
			AppVar = AppVar + 1
		end if
Next

Well, the ExecQuery, as you'll know, takes a WQL query and runs it against the CIMV2 repository. The results are then sent back. You can trim down the size of the result very easily by adding a WHERE clause to your query:

SELECT Caption FROM Win32_Processs WHERE Caption = 'notepad.exe'

Notice I'm also no longer selecting *, but just Caption. If notepad is running, you get one result back for each instance of it, but if it's not, you get an empty collection. All that will be sent back is the Caption property, which is really all you're interested in here anyway.

This doesn't allow you to count other processes in AppVar as you do, so if that's an important feature of your script you'll have to leave the WHERE clause off your WQL query, but you can still SELECT Caption rather than SELECT * which will help a little.

(I'm not sure if this is possible with WQL because I haven't tried it, but it looks like this script wouldn't care if there were 2 or 5 notepad.exe instances running; it only takes action if there are none so 1 or 4 or 25,000 are all equivalent from its point of view so you could use SELECT DISTINCT Caption instead of just SELECT Caption. Like I say, I don't know if this works with WQL but if it's like SQL this will mean you'll get a maximum result collection of one record, no matter how many notepad.exes are running, which again trims down the result size...I know I'm getting a bit obsessive compulsive about this; I mean, what are we talking about, ten bytes?; but still, it's the principle of the thing isn't it? )

Reducing the result set like this helps a lot of you're doing this over a WAN, especially if the remote machine is running a relatively large number of processes.

In this case, if you are only interested in starting notepad if there are no instances of it running, you could actually avoid iterating over the result collection with For Each; all you care about is whether the returned collection is empty. I can't recall off the top of my head whether you use the Count or the Length property (Count, I think) to find this out, but you could just do something like:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
If colItems.Count = 0 Then
    ' Start notepad here
End If

Hope this helps!
__________________
"I'm not a genius. Why do I have to suffer?"
mackenga is offline   Reply With Quote