How do I Clear Collected WMI Data

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

univistaAsked:
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.

merowingerCommented:

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
univistaAuthor Commented:
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
jostranderCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jostranderCommented:
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
jostranderCommented:
removed your refresher too, didn't think it was really necessary here, hope you don't mind
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
univistaAuthor Commented:
Thanks so much.  I don't mind any of the rewrites as I probably had it wrong to begin with.  Points awarded.
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.