Problems executing VBS script in Windows 2000

The next script that must kill dllhost when it consumes over 80% of CPU keeps returning the same error:

Script:
strProcess = "dllhost"
intLimit = 80
strLogFile = "C:\Temp\dllhost.log"

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
While True
	Set colPerf = objWMI.ExecQuery("Select * From Win32_PerfFormattedData_PerfProc_Process Where Name='" & strProcess & "' Or Name like '" & strProcess & "#%'")
	If colPerf.Count = 0 Then
		Wscript.StdErr.WriteLine "No Instance(s) Available."
	Else
		For Each objPerf in colPerf
			intPercentProcessorTime = CInt(objPerf.PercentProcessorTime)
			If intPercentProcessorTime > intLimit Then
				Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)
				objLog.WriteLine "[" & Date & "][" & Time & "] Killed " & strProcess & " with PID " & objPerf.IDProcess & "."
				objLog.Close
				Set colProcess = objWMI.ExecQuery("Select * From Win32_Process Where ProcessId='" & objPerf.IDProcess & "'")
				For Each objProcess in colProcess
					objProcess.Terminate
				Next
			Else
				Wscript.StdOut.WriteLine "Usage for PID " & objPerf.IDProcess & " at " & objPerf.PercentProcessorTime & " at " & Time
			End If
		Next
	End If
	Wscript.Sleep 60 * 1000
Wend

Open in new window


Error:
C:\>cscript.exe /nologo "C:\myscript.vbs"
C:\myscript.vbs(10, 2) SWbemObjectSet: Non Specified Error
LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fareed Ali KhanCommented:
According to the following link:

https://technet.microsoft.com/en-us/library/ee198934.aspx

You can't use the

         colPerf.Count

After

         Set colPerf = objWMI.ExecQuery(

Because it is forward only return type. Count will took the result of query to the end.

So the modified script would be:

strProcess = "dllhost"
intLimit = 80
strLogFile = "C:\Temp\dllhost.log"

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
While True
      Set colPerf = objWMI.ExecQuery("Select * From Win32_PerfFormattedData_PerfProc_Process Where Name='" & strProcess & "' Or Name like '" & strProcess & "#%'")

            For Each objPerf in colPerf
                  intPercentProcessorTime = CInt(objPerf.PercentProcessorTime)
                  If intPercentProcessorTime > intLimit Then
                        Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)
                        objLog.WriteLine "[" & Date & "][" & Time & "] Killed " & strProcess & " with PID " & objPerf.IDProcess & "."
                        objLog.Close
                        Set colProcess = objWMI.ExecQuery("Select * From Win32_Process Where ProcessId='" & objPerf.IDProcess & "'")
                        For Each objProcess in colProcess
                              objProcess.Terminate
                        Next
                  Else
                        Wscript.StdOut.WriteLine "Usage for PID " & objPerf.IDProcess & " at " & objPerf.PercentProcessorTime & " at " & Time
                  End If
            Next

      Wscript.Sleep 60 * 1000

Wend
0
celticianAuthor Commented:
Ok, im trying today as soon as i get to work? have you tried it yet??
0
celticianAuthor Commented:
Thanks for the answer, after making the suggested changes and executing the script i get now the next error:

C:\>cscript.exe /nologo "C:\myscript.vbs"
C:\myscript.vbs(11, 13) 0x80041017

With that hex value at the end... i have checked over google and seems to be something related to WMI, but i really don't know how to handle this... :S
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

celticianAuthor Commented:
Can any one shed some light please?
0
Fareed Ali KhanCommented:
The error is the result of the query returning a null result. Maybe you're specifying something that is not supported in the  Win32_PerfFormattedData_PerfProc_Process under Window 2000.
0
celticianAuthor Commented:
It simply has to kill dllhost when it exceeds 80% of CPU, doesn't require any program or specifical config, should be able to be executed on any win2000 server...
0
Fareed Ali KhanCommented:
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set PerfProcess = objWMI.Get("Win32_PerfFormattedData_PerfProc_Process.Name='dllhost'")
 
 do until PerfProcess.PercentProcessorTime < 80
 
      if PerfProcess.PercentProcessorTime > 80 Then
              wscript.echo "Above 80%"
              PerfProcess.Terminate
      End If

      Wscript.Sleep 60 * 1000

 loop
 

Try the above script.
0
celticianAuthor Commented:
In this case i get the next error:

myscript.vbs(2, 1) SWbemServices: Non Valid Class

:(
0
Fareed Ali KhanCommented:
Did you use the script I give? or is there something else in it except my code? Because I test it and it is working fine on my machine.
0
celticianAuthor Commented:
Yes i used it exactly as it its, is there any special instruction on compilation?

maybe should i change any settings in the server config??
0
Fareed Ali KhanCommented:
It seems your machine don't have certain WMI files. Have a look at the following link:

http://networkadminkb.com/KB/a73/wmi-invalid-class-error-caused-by-missing-files.aspx

Or you can try this script on any other machine.
0
celticianAuthor Commented:
I have taken a look at that link however i really don't want to change any configuration as i don't have the knowledge to fix any damage or i dont know what can happen... there is an important application running on that server used by hundreds of people everyday...

This is the version of the Win 2000 we have installed:
0
celticianAuthor Commented:
win2000
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That class Win32_PerfFormattedData_PerfProc_Process does indeed not exist on W2000 (see http://www.it-visions.de/scripting/WMIReferenz.asp?C_Klasse=Win32_PerfFormattedData_PerfProc_Process and http://www.monitis.com/blog/2012/08/01/the-wmi-usage-peculiarities-at-monitoring/ for confirmation), as all Formatted derivates are only available starting with XP.
The Raw variants however only get the actual, non-averaged data, so a short peak will be taken seriously.
You can try if changing to Win32_PerfRawData_PerfProc_Process works for you as intended, but I suppose it is overreacting.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fareed Ali KhanCommented:
Yes you can replace Win32_PerfFormattedData_PerfProc_Process with Win32_PerfRawData_PerfProc_Process.

http://computer-programming-forum.com/59-vbscript/789e84bf5a91ddfb.htm
0
celticianAuthor Commented:
Now it detect it, thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.