?
Solved

How do I Clear Collected WMI Data

Posted on 2010-03-23
6
Medium Priority
?
562 Views
Last Modified: 2012-05-09
I am attempting to collect WMI data using a vbscript.  I think I've got most of it put together but when I run the script in a loop it repeats the previously collected information in the next line.  I am hoping someone can take a quick look and provide me with the solution to clear the query data and only append the new data to the file.

Forgive the short sleep time as it's only for testing purposes.  
Option Explicit

Const ForAppending = 8 'Contents not overwritten.
 
Dim oWsh, oWshSysEnv, objFSO, objWMIService, objRefresher, objProcessor
Dim oDrives, oDrive, objOutFile, colItems, objItem
Dim strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse, strComputer
 
strComputer = "."
Set oWsh = WScript.CreateObject("WScript.Shell")
Set oWshSysEnv = oWsh.Environment("PROCESS")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
'objRefresher.Refresh

 
strLineTime = Date() & Space(2) & Time()
 
Do
objRefresher.DeleteAll()
'Gets CPU 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")
For Each objItem In colItems   
 strLineProcessorTime = strLineProcessorTime & " " & objItem.PercentProcessorTime
Next
 
'Gets RAM
Set colItems = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_PerfFormattedData_PerfOS_Memory ")
For Each objItem In colItems   
 strLinePercentCommittedBytesInUse = strLinePercentCommittedBytesInUse & " " & objItem.PercentCommittedBytesInUse
Next
'Gets FREE SPACE Report
Set oDrives = objFSO.Drives
For Each oDrive In oDrives
 Select Case oDrive.DriveType
  Case 2 'Fixed Drives    
   strLineDriveSpace = strLineDriveSpace & " " & oDrive.DriveLetter & "\: " & Round(oDrive.FreeSpace / (1024 * 1024)) & "MB free (" & Round(100 * (oDrive.FreeSpace / oDrive.TotalSize), 2) & " %) "
 End Select
Next

'Output to text
Set objOutFile = objFSO.OpenTextFile("C:\cpu_log\hdd_cpu_ram_MON.csv", ForAppending, True)
objOutFile.WriteLine "Time,Computer Name,Processor Usage (%),Memory Usage (%),Drive Free Space"
objOutFile.WriteLine strLineTime & "," & oWshSysEnv("COMPUTERNAME") & "," & strLineProcessorTime & "," & strLinePercentCommittedBytesInUse & "," & strLineDriveSpace
objOutFile.Close
Set objOutFile = Nothing

'objRefresher.Refresh
Wscript.Sleep 3000 ' Sleep in mSec.
Loop

Open in new window

0
Comment
Question by:univista
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 28384719

Option Explicit

Const ForAppending = 8 'Contents not overwritten.

Dim oWsh, oWshSysEnv, objFSO, objWMIService, objRefresher, objProcessor
Dim oDrives, oDrive, objOutFile, colItems, objItem
Dim strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse, strComputer

strComputer = "."
Set oWsh = WScript.CreateObject("WScript.Shell")
Set oWshSysEnv = oWsh.Environment("PROCESS")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
'objRefresher.Refresh


strLineTime = Date() & Space(2) & Time()

Do
objRefresher.DeleteAll()
'Gets CPU
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")
For Each objItem In colItems
strLineProcessorTime = strLineProcessorTime & " " & objItem.PercentProcessorTime
Next
set colItems = Nothing

'Gets RAM
Set colItems = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_PerfFormattedData_PerfOS_Memory ")
For Each objItem In colItems
strLinePercentCommittedBytesInUse = strLinePercentCommittedBytesInUse & " " & objItem.PercentCommittedBytesInUse
Next
set colItems = Nothing

'Gets FREE SPACE Report
Set oDrives = objFSO.Drives
For Each oDrive In oDrives
Select Case oDrive.DriveType
Case 2 'Fixed Drives
strLineDriveSpace = strLineDriveSpace & " " & oDrive.DriveLetter & "\: " & Round(oDrive.FreeSpace / (1024 * 1024)) & "MB free (" & Round(100 * (oDrive.FreeSpace / oDrive.TotalSize), 2) & " %) " 
End Select
Next

'Output to text
Set objOutFile = objFSO.OpenTextFile("C:\cpu_log\hdd_cpu_ram_MON.csv", ForAppending, True)
objOutFile.WriteLine "Time,Computer Name,Processor Usage (%),Memory Usage (%),Drive Free Space"
objOutFile.WriteLine strLineTime & "," & oWshSysEnv("COMPUTERNAME") & "," & strLineProcessorTime & "," & strLinePercentCommittedBytesInUse & "," & strLineDriveSpace
objOutFile.Close
Set objOutFile = Nothing

'objRefresher.Refresh
Wscript.Sleep 3000 ' Sleep in mSec.
Loop
0
 

Author Comment

by:univista
ID: 28385323
Thanks for the quick try but that's not going to do it.  That removes the collected data before it even hits the output file.  
0
 
LVL 12

Expert Comment

by:jostrander
ID: 28387553
Merowinger's got the right idea, it just needs to be taken a bit further.  

You've got some strings in there too that should be reset as well.  Try this out:
Option Explicit

Const ForAppending = 8 'Contents not overwritten.
 
Dim oWsh, oWshSysEnv, objFSO, objWMIService, objProcessor
Dim oDrives, oDrive, objOutFile, colProc,colMemory, objItem
Dim strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse, strComputer,AddHeaders
 
strComputer = "."
Set oWsh = WScript.CreateObject("WScript.Shell")
Set oWshSysEnv = oWsh.Environment("PROCESS")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 

Do
	
	strLineTime = Date() & Space(2) & Time()
	
	'Gets CPU 
	Set colProc = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")
	For Each objItem In colProc   
	 strLineProcessorTime = strLineProcessorTime & " " & objItem.PercentProcessorTime
	Next
	 
	'Gets RAM
	Set colMemory = objWMIService.ExecQuery("Select * FROM Win32_PerfFormattedData_PerfOS_Memory ")
	For Each objItem In colMemory   
	 strLinePercentCommittedBytesInUse = strLinePercentCommittedBytesInUse & " " & objItem.PercentCommittedBytesInUse
	Next
	'Gets FREE SPACE Report
	Set oDrives = objFSO.Drives
	For Each oDrive In oDrives
	 Select Case oDrive.DriveType
	  Case 2 'Fixed Drives    
	   strLineDriveSpace = strLineDriveSpace & " " & oDrive.DriveLetter & "\: " & Round(oDrive.FreeSpace / (1024 * 1024)) & "MB free (" & Round(100 * (oDrive.FreeSpace / oDrive.TotalSize), 2) & " %) "
	 End Select
	Next
	
	'Output to text
	If objFSO.FileExists("C:\cpu_log\hdd_cpu_ram_MON.csv") then
		AddHeaders=False
	Else
		AddHeaders=True
	End If
	Set objOutFile = objFSO.OpenTextFile("C:\cpu_log\hdd_cpu_ram_MON.csv", ForAppending, True)
	If AddHeaders=True then
		objOutFile.WriteLine "Time,Computer Name,Processor Usage (%),Memory Usage (%),Drive Free Space"
	End If
	objOutFile.WriteLine strLineTime & "," & oWshSysEnv("COMPUTERNAME") & "," & strLineProcessorTime & "," & strLinePercentCommittedBytesInUse & "," & strLineDriveSpace
	objOutFile.Close
		
	'Cleanup
	Set objOutFile = Nothing
	Set oDrives=nothing
	Set colProc=nothing
	Set colMemory=nothing
	strLineProcessorTime=""
	strLineDriveSpace=""
	strLineTime=""
	strLinePercentCommittedBytesInUse=""
	
	Wscript.Sleep 3000 ' Sleep in mSec.
	'wscript.echo "looping..." & now
	
Loop

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 12

Expert Comment

by:jostrander
ID: 28387713
I moved some other stuff around too...
moved timestamps into the loop
CSV headers only if it's a new file
distinct collection names
0
 
LVL 12

Accepted Solution

by:
jostrander earned 2000 total points
ID: 28388021
removed your refresher too, didn't think it was really necessary here, hope you don't mind
0
 

Author Closing Comment

by:univista
ID: 31706324
Thanks so much.  I don't mind any of the rewrites as I probably had it wrong to begin with.  Points awarded.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question