We help IT Professionals succeed at work.

VBScript WMI old Values get reused

456 Views
Last Modified: 2014-05-22
Dear All,

I am using a script to monitor our 2003 Server Disk Space and just stumbled over a bug.
I am pulling the Disk Space of each logical Drive via WMI which has been working fine, the issue is that if for what ever reason WMI dos not get a value, it reuses the previous received value.

For Example I have:
Server A:
Disk C 500GB and 200 free
Disk E 200GB and 50 free

Server B:
Is powered off

Server C:
Disk C 300GB and 200 free
Disk E 100GB and 50 free


My Result would be:
Server A:
Disk C 500GB and 200 free
Disk E 200GB and 50 free

Server B:
Disk C 500GB and 200 free (Value from Server A)
Disk E 200GB and 50 free (Value from Server A)

Server C:
Disk C 300GB and 200 free
Disk E 100GB and 50 free

How can I prevent theWMI object to use the old Values?
 
My code looks like this:

On Error Resume Next 
Const ForAppending = 2 
Const HARD_DISK = 3 
Const ForReading = 1 
 
'Declaring the variables 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set SrvList = objFSO.OpenTextFile("Server_List.txt", ForReading) 
Set ReportFile = objFSO.OpenTextFile ("D:\WEB\DiskSpace.html", ForAppending, True) 
i = 0 
 
'Initializing the HTML Tags for better formatting 
ReportFile.writeline("<html>") 
ReportFile.writeline("<head>") 
ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>") 
ReportFile.writeline("<title>" & " Servers Disk Space Report "& Date & " , " & Time &"</title>") 
ReportFile.writeline("<style type='text/css'>") 
ReportFile.writeline("<!--") 
ReportFile.writeline("td {") 
ReportFile.writeline("font-family: Tahoma;") 
ReportFile.writeline("font-size: 11px;") 
ReportFile.writeline("border-top: 1px solid #999999;") 
ReportFile.writeline("border-right: 1px solid #999999;") 
ReportFile.writeline("border-bottom: 1px solid #999999;") 
ReportFile.writeline("border-left: 1px solid #999999;") 
ReportFile.writeline("padding-top: 0px;") 
ReportFile.writeline("padding-right: 0px;") 
ReportFile.writeline("padding-bottom: 0px;") 
ReportFile.writeline("padding-left: 0px;") 
ReportFile.writeline("}") 
ReportFile.writeline("body {") 
ReportFile.writeline("margin-left: 5px;") 
ReportFile.writeline("margin-top: 5px;") 
ReportFile.writeline("margin-right: 0px;") 
ReportFile.writeline("margin-bottom: 10px;") 
ReportFile.writeline("") 
ReportFile.writeline("table {") 
ReportFile.writeline("border: thin solid #000000;") 
ReportFile.writeline("}") 
ReportFile.writeline("-->") 
ReportFile.writeline("</style>") 
ReportFile.writeline("</head>") 
ReportFile.writeline("<body>") 
 
ReportFile.writeline("<table width='50%'>") 
ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
ReportFile.writeline("<td colspan='7' height='25' align='center'>") 
ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>Disk Space Report  "& Date & " , " & Time &"</strong></font>") 
ReportFile.writeline("</td>") 
ReportFile.writeline("</tr>") 
ReportFile.writeline("</table>") 
 
 
'Declaring the Server Name for report generation 
Do Until SrvList.AtEndOfStream 
    StrComputer = SrvList.Readline 
 
    ReportFile.writeline("<table width='50%'><tbody>") 
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
    ReportFile.writeline("<td width='50%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong>" & StrComputer & "</strong></font></td>") 
    ReportFile.writeline("</tr>") 
 
 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") 
 
    ReportFile.writeline("<tr bgcolor=#CCCCCC>") 
        ReportFile.writeline("<td width='05%' align='center'>Drive / Mount</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Total Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Used Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Free Space (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Freespace %</td>") 
    ReportFile.writeline("</tr>") 
 
        'Starting the loop to gather values from all Hard Drives 
        For Each objDisk in colDisks 
 
            'Delcaring the Variables


            TotSpace=Round(((objDisk.Size)/1073741824),2) 
            FrSpace=Round(objDisk.FreeSpace/1073741824,2) 
            FrPercent=Round((FrSpace / TotSpace)*100,0) 
            UsSpace=Round((TotSpace - FrSpace),2) 
            Drv=objDisk.DeviceID 
            VolName=objDisk.DeviceID 
 
            'Lnt=Len(VolName) 
 
            'If  Len(VolName) =  3 then 
                If FrPercent > 20 Then 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>" 
                ElseIf FrPercent < 10 Then 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
                Else 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
                End If 
            'Else 
            'End If 
        Next 
 
    ReportFile.writeline("<tr>") 
    ReportFile.writeline("<td width='50%' colSpan=6>&nbsp;</td>") 
    ReportFile.writeline("</tr>") 
 
    ReportFile.writeline("</tbody></table>") 
Loop 
ReportFile.WriteLine "</body></html>"

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
DavidSystems Engineer

Author

Commented:
Thanks MacroShadow! and that solves the issue but brings me to another, I have the same problem with logical drives,
Say server X has

Drive C with 90GB
Drive E with 180Gb
Drive F with 70GB

Every now end then I get results like:

Drive C with 90GB
Drive E with 90GB
Drive F with 70GB

I am almost sure that this is do to the WMI querry on Drive E Failing for what ever reason and the Script just reuses the value it received before on the C Drive.

I tried emptying my Variables  like:
      TotSpace=""
      FrSpace=""
      FrPercent=""
      UsSpace=""
      Drv=""
      VolName=""

Before assigning values but it had no impact.
DavidSystems Engineer

Author

Commented:
I think I got it, probably not the most elegant way but I think this will do.
 See additions in Line 66, 67, 82, 87

On Error Resume Next 
Const ForAppending = 2 
Const HARD_DISK = 3 
Const ForReading = 1 
 
'Declaring the variables 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set SrvList = objFSO.OpenTextFile("Server_List.txt", ForReading) 
Set ReportFile = objFSO.OpenTextFile ("D:\WEBsites\ServerMonitoringSite\DiskSpace.html", ForAppending, True) 
i = 0 
 
'Initializing the HTML Tags for better formatting 
ReportFile.writeline("<html>") 
ReportFile.writeline("<head>") 
ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>") 
ReportFile.writeline("<title>" & "Disk Space Report "& Date & " , " & Time &"</title>") 
ReportFile.writeline("<style type='text/css'>") 
ReportFile.writeline("<!--") 
ReportFile.writeline("td {") 
ReportFile.writeline("font-family: Tahoma;") 
ReportFile.writeline("font-size: 11px;") 
ReportFile.writeline("border-top: 1px solid #999999;") 
ReportFile.writeline("border-right: 1px solid #999999;") 
ReportFile.writeline("border-bottom: 1px solid #999999;") 
ReportFile.writeline("border-left: 1px solid #999999;") 
ReportFile.writeline("padding-top: 0px;") 
ReportFile.writeline("padding-right: 0px;") 
ReportFile.writeline("padding-bottom: 0px;") 
ReportFile.writeline("padding-left: 0px;") 
ReportFile.writeline("}") 
ReportFile.writeline("body {") 
ReportFile.writeline("margin-left: 5px;") 
ReportFile.writeline("margin-top: 5px;") 
ReportFile.writeline("margin-right: 0px;") 
ReportFile.writeline("margin-bottom: 10px;") 
ReportFile.writeline("") 
ReportFile.writeline("table {") 
ReportFile.writeline("border: thin solid #000000;") 
ReportFile.writeline("}") 
ReportFile.writeline("-->") 
ReportFile.writeline("</style>") 
ReportFile.writeline("</head>") 
ReportFile.writeline("<body>") 
 
ReportFile.writeline("<table width='50%'>") 
ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
ReportFile.writeline("<td colspan='7' height='25' align='center'>") 
ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>Disk Space Report  "& Date & " , " & Time &"</strong></font>") 
ReportFile.writeline("</td>") 
ReportFile.writeline("</tr>") 
ReportFile.writeline("</table>") 
 
 
'Declaring the Server Name for report generation 
Do Until SrvList.AtEndOfStream 
    StrComputer = SrvList.Readline 
 
    ReportFile.writeline("<table width='50%'><tbody>") 
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
    ReportFile.writeline("<td width='50%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong>" & StrComputer & "</strong></font></td>") 
    ReportFile.writeline("</tr>") 
 
    Set objWMIService = Nothing
    Set ColDisks = Nothing 

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
    Set previousValue = objDisk
 
    ReportFile.writeline("<tr bgcolor=#CCCCCC>") 
        ReportFile.writeline("<td width='05%' align='center'>Drive / Mount</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Total Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Used Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Free Space (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Freespace %</td>") 
    ReportFile.writeline("</tr>") 
 
	Set previousValue = objDisk

        'Starting the loop to gather values from all Hard Drives 
       
	For Each objDisk in colDisks 
		If Not previousValue = objDisk then
             
 		
	            'Delcaring the Variables


	            TotSpace=Round(((objDisk.Size)/1073741824),2) 
        	    FrSpace=Round(objDisk.FreeSpace/1073741824,2) 
           	 FrPercent=Round((FrSpace / TotSpace)*100,0) 
            	UsSpace=Round((TotSpace - FrSpace),2) 
           	 Drv=objDisk.DeviceID 
            	VolName=objDisk.DeviceID 
 
 	           'Lnt=Len(VolName) 
 
 	           'If  Len(VolName) =  3 then 
        	        If FrPercent > 20 Then 
                	    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>" 
            	    ElseIf FrPercent < 10 Then 
                	    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
             	   Else 
                	    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
   	             End If 
        	    'Else 
            	'End If
	End If 
        Next 
   
    Set objWMIService = Nothing
    Set ColDisks = Nothing 
    ReportFile.writeline("<tr>") 
    ReportFile.writeline("<td width='50%' colSpan=6>&nbsp;</td>") 
    ReportFile.writeline("</tr>") 
 
    ReportFile.writeline("</tbody></table>") 
Loop 
ReportFile.WriteLine "</body></html>"

Open in new window

CERTIFIED EXPERT

Commented:
Nice workaround, although I'm not sure why you encountered the problem in the first place.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.