Link to home
Start Free TrialLog in
Avatar of martin05
martin05

asked on

Checking disk space errors vbscript

Hi
I have a little bit of VB which checks diskspace

Sub chkdiskspace (StrComp)

If dbgTitle <> "" Then objdiv.innerhtml = strMsg & "<font face=" & strFontStyle & " color=" & strFontColor2& ">" _
& "Checking disk space...</font><br>"
l.writeline Time & " - Checking disk space."
'==Connect to winmgmts==
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComp & "\root\cimv2")


'==Run query to identify logical disks==
Set colDisks = objWMIService.ExecQuery _
("Select Freespace, Name, Size from Win32_LogicalDisk where DriveType = 3")
Call fctErrorHandling

'==Write header to htm file==
res.Writeline ("<table border=1 width=100% cellspacing=0 cellpadding=3>")
res.Writeline ("<tr>")
res.Writeline ("<th bgcolor=" & strHDRColor1 & " colspan=6 width=200>")
res.Writeline ("<p align=left>")
res.Writeline ("<b><font face=" & strFontStyle & " size=2 color=" & strFontColor2 & ">Disk space</font></b></p>")
res.Writeline ("</th>")
res.Writeline ("</tr>")
res.Writeline ("<tr>")
res.Writeline ("<th bgcolor=" & strHDRColor2 & " width=60><font face=" & strFontStyle & " color=" & strFontColor1 & " size=1>Disk ID</font></th>")
res.Writeline ("<th bgcolor=" & strHDRColor2 & " width=60><font face=" & strFontStyle & " color=" & strFontColor1 & " size=1>Percentage Free Space</font></th>")
res.Writeline ("<th bgcolor=" & strHDRColor2 & " width=60><font face=" & strFontStyle & " color=" & strFontColor1 & " size=1>Free Space</font></th>")
res.Writeline ("<th bgcolor=" & strHDRColor2 & " width=60><font face=" & strFontStyle & " color=" & strFontColor1 & " size=1>Total Space</font></th>")
res.Writeline ("</tr>")

'==Write results to htm file==
For Each objDisk In colDisks
Call bgcolor
res.Writeline ("<TR bgcolor='" & strbgcolor & "'><TD><font face=" & strFontStyle & " color=" & strFontColor2 & " size=1> " & objDisk.Name & strendline)

If (objDisk.Freespace/objDisk.Size)*100 <=10 Then

fontcol = "#FF0000"
Else
fontcol = "#666666"
End If
If objDisk.Freespace > 10000 Then
    strUnit = "GB"
    strDiskSize = FormatNumber(objDisk.Freespace / 1073741824,2)
    strDiskTotal = FormatNumber(objDisk.Size / 1073741824,2)
ElseIf objDisk.Freespace < 1 Then
    strUnit = "KB"
    strDiskSize = FormatNumber(objDisk.Freespace / 1024,2)
    strDiskTotal = FormatNumber(objDisk.Size / 1024,2)
ElseIf objDisk.Freespace < 1000 Then
    strUnit = "MB"
    strDiskSize = FormatNumber(objDisk.Freespace/1048576,2)
    strDiskTotal = FormatNumber(objDisk.Size / 1048576,2)
End If
strPercentFree = Round((objDisk.Freespace/objDisk.Size)*100,2)


If strPercentFree < intFreeSpaceThreshold Then
'wscript.echo strPercentFree
strSpace = strSpace & objDisk.Name & " - " & Round((objDisk.Freespace/objDisk.Size)*100,2) _
& "% free (" & FormatNumber(objDisk.Freespace / 1073741824,2) & "Gb remaining)<br>"
    res.Writeline ("<TD><font face=" & strFontStyle & " color='red' size=1>" & strPercentFree & "%" & strendline)
    blnErrors = True
    blnDiskSpace = True
Else
    res.Writeline ("<TD><font face=" & strFontStyle & " color=" & strfontcolor2 & " size=1>" & strPercentFree & "%" & strendline)
End If

res.Writeline ("<TD><font face=" & strFontStyle & " color=" & strfontcolor2 & " size=1>" & strDiskSize & strUnit & strendline)
res.Writeline ("<TD><font face=" & strFontStyle & " color=" & strfontcolor2 & " size=1>" & strDiskTotal & strUnit & strendline & "</TR>")

Next

 the problem i have is I need to omit some drives from the check as they are returning NULL values
can anyone see from the code an easy way to do it ?

thanks


Avatar of sirbounty
sirbounty
Flag of United States of America image

Change your query:

Set colDisks = objWMIService.ExecQuery _
("Select Freespace, Name, Size from Win32_LogicalDisk where DriveType = 3 And Name <> '" & "G:" & "'")
Avatar of martin05
martin05

ASKER

Excellent that works
can i ask if i wanted to add more than 1 exception how do I do that ?
Where is the majority?

You can use
Name = "C:"
if that's all you want to check
or
Name = "C:" Or "D:"

The reverse is also true Name <> "C:" or "D:"
I need to omit J K and L I am trying pretty much what you have told me but I keep getting errors in my syntax
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America 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
Brilliant sirbounty
thanx
Happy to help - thanx for the grade! :^ )