×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

DiskSpace Monitoring Script

DiskSpace Monitoring Script

DiskSpace Monitoring Script

(OP)
I have a really useful Script that will read from a list of Servers and provide the Disk Space utilization report,
The only issue is that if a Server is offline or not available it will list the previous servers results instead of an error or no value.

I could really use some help amending the script to reflect if a server is unavailable.


On Error Resume Next
Const ForAppending = 8
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 ("Diskspace_status.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>" & "EMEA Servers Disk Space Report</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>EMEA Servers Disk Space Report</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 > 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='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>"
ElseIf FrPercent < 5 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='#ff00fb' 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='##00ffff' 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>"

RE: DiskSpace Monitoring Script

On Error Resume Next suppresses runtime errors, which will cause unexpected results like you are seeing. First step is to remove that line at the top of the script so you can see which line is giving you a runtime error.

Now, to trap errors, every line that could come back with a runtime error should be surrounded with code like this or something similar:

CODE

On Error Resume Next 'suppress runtime errors temporarily
Execute the line of code here
If Err.Number <> 0 Then
   'execute code to recover from the error
End If
On Error Goto 0 'go back to stopping on a runtime error 

Assuming the "Set objWMIService = etc" line is the one generating the runtime error if the server is offline, you could use something like this:

CODE

Do Until SrvList.AtEndOfStream
   ...
   On Error Resume Next 'suppress runtime errors
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
   If Err.Number <> 0 Then 'if we got an error...
      On Error Goto 0 'don't suppress runtime errors
   Else 'if no errors...
      On Error Goto 0 'don't suppress runtime errors, and execute code for this server
      ...
      ...
      ...
   End If
Next 

Also consider just pinging the server to see if it accessible:
FAQ329-6527: How do I ping or resolve to an IP address within a VBScript?

RE: DiskSpace Monitoring Script

(OP)
Thanks for your reply Gutiarzan, I am a little unsure how to put all of what you provided into the script.
Could you advise where I should enter the 2nd part of Code you provided.

I would like the output against the server name to say something like "unavailable" or "offline"

RE: DiskSpace Monitoring Script

Something like this, not tested

CODE

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>")

   On Error Resume Next
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
   If Err.Number <> 0 Then
      On Error Goto 0
      ReportFile.writeline( ... code your "server offline" message ... )
   Else
      On Error Goto 0
      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 > 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='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>"
         ElseIf FrPercent < 5 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='#ff00fb' 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='##00ffff' align=center>" & FrPercent & "%" &"</td></tr>"
      
         End If
         'Else
         'End If

      Next

   End If


   ReportFile.writeline("<tr>")
   ReportFile.writeline("<td width='50%' colSpan=6> </td>")
   ReportFile.writeline("</tr>")

   ReportFile.writeline("</tbody></table>")

Loop 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close