none
VB Script to send email when diskspace LOW RRS feed

  • Question

  • Below script produces a report and mail sent successfully,  but no disk space details in the body, only table with headings.

    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("c:\temp\sl.txt", ForReading)
    Set ReportFile = objFSO.CreateTextFile ("c:\temp\diskspace.htm")
    ReportFile.Close
    Set ReportFile = objFSO.OpenTextFile ("c:\temp\diskspace.htm", 2)

    ReportFile.writeline("<html>")
    ReportFile.writeline("<head>")
    ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>")
    ReportFile.writeline("<title>" & "DISKSPACE 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='100%'>")
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>")
    ReportFile.writeline("<td colspan='7' height='25' align='center'>")
    ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>DISKSPACE Report</strong></font>")
    ReportFile.writeline("</td>")
    ReportFile.writeline("</tr>")
    ReportFile.writeline("</table>")

     

    Do Until SrvList.AtEndOfStream
     StrComputer = SrvList.Readline


     ReportFile.writeline("<table width='100%'><tbody>")
     ReportFile.writeline("<tr bgcolor='#CCCCCC'>")
     ReportFile.writeline("<td width='100%' 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_Volume")

     ReportFile.writeline("<tr bgcolor=#CCCCCC>")
      ReportFile.writeline("<td width='10%' align='center'>Drive / Mount</td>")
      ReportFile.writeline("<td width='50%' align='center'>Drive Label</td>")
      ReportFile.writeline("<td width='10%' align='center'>Total Capacity (in GB)</td>")
      ReportFile.writeline("<td width='10%' align='center'>Used Capacity (in GB)</td>")
      ReportFile.writeline("<td width='10%' align='center'>Free Space (in GB)</td>")
      ReportFile.writeline("<td width='10%' 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.Capacity)/1073741824),2)
       FrSpace=Round(objDisk.FreeSpace/1073741824,2)
       FrPercent=Round((FrSpace / TotSpace)*100,0)
       UsSpace=Round((TotSpace - FrSpace),2)
       Drv=objDisk.DriveLetter
       VolName=objDisk.Label

       If Len(VolName) > 3 then
        If FrPercent > 20 Then
         ReportFile.WriteLine "<tr><td align=left>" & Drv & "</td><td align=left>" & VolName & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td align=center>" & FrPercent & "%" &"</td></tr>"
        ElseIf FrPercent < 10 Then
         ReportFile.WriteLine "<tr><td align=left>" & Drv & "</td><td align=left>" & VolName & "</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=left>" & Drv & "</td><td align=left>" & VolName & "</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='100%' colSpan=6>&nbsp;</td>")
     ReportFile.writeline("</tr>")

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

    'Incrementing the Server List line value and running the routine
    'i = i + 1
    Loop

    ReportFile.WriteLine "</body></html>"
    ReportFile.Close


    SendAlert=TRUE
    VarTo = "irshad@sc.com"
    If SendAlert then
      Set objMessage = CreateObject("CDO.Message")
      objMessage.Subject = "diskspace report" & date() & " " & time()
      objMessage.From = "irshad@cbd.com"
      objMessage.To = VarTo

     ' objMessage.TextBody = "Alert - Excessive user mailbox growth"
      objMessage.CreateMHTMLBody "file://c:\temp\diskspace.htm"
      'objMessage.CreateMHTMLBody "file://c:\temp\OneMail\monitorDumpster\CS_Mailbox_Sizes.html"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

      'Name or IP of Remote SMTP Server
      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "172.11.14.22"

      'Server port (typically 25)
      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

      objMessage.Configuration.Fields.Update

      objMessage.Send

      set objMessage=Nothing
      If ERR.Number<>0 then
       WScript.Echo "ERR: Sending mail"
       ERR.Clear
      End If

    end if

     

    Thanks.

    Tuesday, June 24, 2014 12:51 AM

Answers

  • It would help to change to this:

            Set colDisks = objWMIService.ExecQuery("Select * from Win32_Volume Where DriveType=3")

    And change this line:

    If Len(VolName) > 3 then

    to this:

    If True then


    ¯\_(ツ)_/¯

    • Marked as answer by Ireshad Tuesday, June 24, 2014 9:00 PM
    Tuesday, June 24, 2014 2:22 AM
  • Jrv,

    Thanks alot , it worked.

    Thanks all.

    Ireshad

    That makes it work now but any error will cause it to fail completely.


    ¯\_(ツ)_/¯

    • Marked as answer by Ireshad Friday, June 27, 2014 5:28 PM
    Tuesday, June 24, 2014 10:04 PM

All replies

  • Start by removing "On Error Resume Next"


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 12:57 AM
  • Nop !

    Still no output.

    Tuesday, June 24, 2014 2:11 AM
  • You have to fix all of the errors in the code.


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 2:19 AM
  • To see what is in your report open this file: c:\temp\diskspace.htm


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 2:20 AM
  • It would help to change to this:

            Set colDisks = objWMIService.ExecQuery("Select * from Win32_Volume Where DriveType=3")

    And change this line:

    If Len(VolName) > 3 then

    to this:

    If True then


    ¯\_(ツ)_/¯

    • Marked as answer by Ireshad Tuesday, June 24, 2014 9:00 PM
    Tuesday, June 24, 2014 2:22 AM
  • Hi,

    Are you sure you're passing this test?

    If Len(VolName) > 3

    If not, you won't get any output anyway.


    EDIT: Hah, slow on submit. See above for the same suggestion.

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Tuesday, June 24, 2014 2:25 AM
  • Mike - I told him to remove the test.  It doesn't work.

    I add DiskType = 3 to the WMI which is what he really needs.

    Most of thisis guesswork from some old VBScript code. I recognize the variables and style.

    Those changes worked.

    Yes - that line is most of the issue.

    Any error that occurs will abort the code.  Some tests need to be added.


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 2:31 AM
  • Jrv,

    Thanks alot , it worked.

    Thanks all.

    Ireshad

    Tuesday, June 24, 2014 9:05 PM
  • Jrv,

    Thanks alot , it worked.

    Thanks all.

    Ireshad

    That makes it work now but any error will cause it to fail completely.


    ¯\_(ツ)_/¯

    • Marked as answer by Ireshad Friday, June 27, 2014 5:28 PM
    Tuesday, June 24, 2014 10:04 PM