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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function will only run once then bombs out :( 1

Status
Not open for further replies.

mikejc

MIS
Jul 25, 2004
2
US
Thanks in advance for any help/suggestions...

I wrote a pretty basic function to check how many times a specific process is running on a machine. It passes the host name to a local variable called strComputerName.

Inside the function is a locally set variable called objWMIService. The first time this line is run it works fine, but any additional times it will error out if i pass a different host to it. I can re-run it many times if I never change what is set to objWMIService.

I included the output after the code...
please note if you try to run the code to reproduce, make sure u replace the ip addresses with ones valid for your network

---

dim strComputer(2)

for x = 1 to 2

strComputer(x)="192.168.0.10" & x

ProcessChecker(strComputer(x))

next


function ProcessChecker(strComputerName)
set objWMIService = nothing
msgbox strComputerName
set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'svchost.exe' ")

wscript.echo "svchost.exe is running " & colProcesses.Count & " times on " & strComputerName & "."

end function

---

The output will look like this for run through 1:

popup 1: 192.168.0.101
(used for tracing the error to make sure the host name gets passed through)


popup 2: 'svchost is running on 192.168.0.101 5 times."


Now the second time:

popup 1: 192.168.0.102
the second hostname still passes through fine...once again this is here for testing

popup 2: Line 16
Char 1
Error 0x8004100A
Code 8004100A
Source Null


----

 
Hello mikejc,

Try run only the x=2 case see what the script ends up with.
[tt]
call ProcessChecker("192.168.0.102")
[/tt]
regards - tsuji
 
Thanks tsuji, when i called the second system first, it failed right off the bat. So perhaps it is not the loop, but a problem with it extracting daa from my other pc in my test environment. now i have something to go on, thanks!
 
With WMI you must have Administrator rights on all computors you want to read info from.
Beware that you also have to execute your script as the global administrator.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top