WMI Query

vage

2[H]4U
Joined
Jan 10, 2005
Messages
3,038
So whos good at writing WMI querries? I am basically looking to make some to help me monitor a computers CPU and RAM usage in as close to real time as I can get, and just started reading up on WMI now. I could really use a hand if anyone has some previous experience with this type of thing.
 
perfmon won't work for you?

Can I use perfmon to return a number instead of just open the performance console? I basically need to have a script running, and if the number that it returns is beyond a threshold, it will alert me.
 
strComputer = "."
Just replace the . with a machine name or IP.

Works fairly well with my minimal testing.
 
Shambler you're my hero.

Don't thank me, thank the Technet Scripting Repository heheh.

I'd recommend trying to add a time stamp to the alerts and another check and display for 50% usage.
Ya know, see that the machine is constantly using 50% (Or whatever your normal/baseline usage is) and definitely show an alert for 90% with time stamp.

I'll screw around with it today, but I suck at WQL/SQL :(
 
Yeah I'm definitely not going to just throw this into production and use it, but its an awesome base for me to start making my own config changes with.
 
Added in the Machine name and time stamp (Just for the Processor script so far. Edits in bold.). You will have to manually adjust the strComputer declaration at the top. However this should make the alert easier to read/understand quickly, when it pops up.

You could then make a few copies of this script, adjust the strcomputer declaration and run them side by side on your screen. Ya know, monitor a few computers.


strComputer = "9364SCCMtest1"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh

Do
For Each intProcessorUse in objProcessor
If intProcessorUse.PercentProcessorTime > 90 Then
intThresholdViolations = intThresholdViolations + 1
If intThresholdViolations = 10 Then
intThresholdViolations = 0
Wscript.Echo "Processor usage on machine " & strcomputer &" threshold exceeded."
Wscript.Echo "Time" & Now()
End If
Else
intThresholdViolations = 0
End If
Next
Wscript.Sleep 6000
objRefresher.Refresh
Loop
 
When you copy that out, you may need to adjust the spacing on some of the Echo's.
 
Processor alert with Prompt.

strAnswer = InputBox("Please enter a machine name:", _
"Machine name")
strcomputer = stranswer
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh

Do
For Each intProcessorUse in objProcessor
If intProcessorUse.PercentProcessorTime > 90 Then
intThresholdViolations = intThresholdViolations + 1
If intThresholdViolations = 10 Then
intThresholdViolations = 0
Wscript.Echo "Processor usage on machine " & strcomputer &" threshold exceeded."
Wscript.Echo "Time" & Now()
End If
Else
intThresholdViolations = 0
End If
Next
Wscript.Sleep 6000
objRefresher.Refresh
Loop
 
Back
Top