Link to home
Start Free TrialLog in
Avatar of celtician
celticianFlag for American Samoa

asked on

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
Avatar of Fareed Ali Khan
Fareed Ali Khan
Flag of Australia image

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
Avatar of celtician

ASKER

Ok, im trying today as soon as i get to work? have you tried it yet??
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
Can any one shed some light please?
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.
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...
SOLUTION
Avatar of Fareed Ali Khan
Fareed Ali Khan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In this case i get the next error:

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

:(
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.
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??
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.
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:
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes you can replace Win32_PerfFormattedData_PerfProc_Process with Win32_PerfRawData_PerfProc_Process.

http://computer-programming-forum.com/59-vbscript/789e84bf5a91ddfb.htm
Now it detect it, thank you!