timeiu2009
Technical User
I'm a total vbscript noob and need a little help. A friend of mine gave me the following script that will query a list of servers listed in a text file and generate and email an html report that lists each servers drive space and creates a total at the bottom(I'm not using the email feature at the moment so that's commented out). The report works great, however, the totals at the bottom are off. Anybody know why?
'*********************************************************************************************
'* Script name: FileServerDriveStats.vbs
'* Created on: 8/28/08
'* Last Updated:8/28/08
'* Purpose: Scan servers for disk space information
'*
'*********************************************************************************************
'*
'* Use: cscript FileServerDriveStats.vbs
'*
'* Required 3rd Party executables:
'*
'* none
'*
'* Variables to Edit:
'*
'* strServerList Path to serverlist
'* oobjMessage.From E-mail address of sender
'* objMessage.To E-mail address of receiver
'* objMessage.Subject Subject of email
'* objMessage.TextBody Message body
'* objMessage.AddAttachment Path to the attachment
'* strServerList Path to serverlist
'*
'*
'* NOTE: Users must have administrative access to target systems.
'*
'***********************************************************************************************
On Error Resume Next
'**********************
'* Standard Variables *
'**********************
strServerList = "C:\DiskSpace\serverList.txt"
'**********************
'* Constant Variables *
'**********************
Set objShell = CreateObject("Wscript.Shell")
Dim driveletter(24)
Set fso = CreateObject("Scripting.FileSystemObject")
Set WebFullReport = fso.CreateTextFile("FullDiskReport.htm", True)
Set HostingCriticalReport = fso.CreateTextFile("CriticalHostingDiskReport.htm", True)
strSendAppEmail = "false"
strSendHostingEmail = "false"
initDrives
addtop
'**********************
'* Setup HTML Table *
'**********************
WebFullReport.WriteLine ("<table border=1>")
WebFullReport.WriteLine ("<tr>")
WebFullReport.WriteLine ("<td><b>Server Name </b></td>")
WebFullReport.WriteLine ("<td><b>Drive Letter</b></td>")
WebFullReport.WriteLine ("<td><b>Total Size</b></td>")
WebFullReport.WriteLine ("<td><b>Free Space</b></td>")
WebFullReport.WriteLine ("<td><b>% Free</b></td>")
WebFullReport.WriteLine ("<td><b>Used Space</b></td>")
WebFullReport.WriteLine ("</tr>")
AppCriticalReport.WriteLine ("<table border=1>")
AppCriticalReport.WriteLine ("<tr>")
AppCriticalReport.WriteLine ("<td><b>Server Name </b></td>")
AppCriticalReport.WriteLine ("<td><b>Drive Letter</b></td>")
AppCriticalReport.WriteLine ("<td><b>Total Size</b></td>")
AppCriticalReport.WriteLine ("<td><b>Free Space</b></td>")
AppCriticalReport.WriteLine ("<td><b>% Free</b></td>")
AppCriticalReport.WriteLine ("<td><b>Used Space</b></td>")
AppCriticalReport.WriteLine ("</tr>")
HostingCriticalReport.WriteLine ("<table border=1>")
HostingCriticalReport.WriteLine ("<tr>")
HostingCriticalReport.WriteLine ("<td><b>Server Name </b></td>")
HostingCriticalReport.WriteLine ("<td><b>Drive Letter</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Total Size</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Free Space</b></td>")
HostingCriticalReport.WriteLine ("<td><b>% Free</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Used Space</b></td>")
HostingCriticalReport.WriteLine ("</tr>")
'**********************
'* Scan Servers *
'**********************
Set SERVERS = fspenTextFile(strServerList, 1)
Do While SERVERS.AtEndOfStream <> True
server = SERVERS.Readline
Set objScriptExec = objShell.Exec("ping -n 1 -w 1000 " & server)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
usedtotal = usedtotal + serverusedtotal
total = total + totalsize
servertotalsize = 0
serverusedtotal = 0
serveravailsize = 0
for I=1 To 24
if fso.DriveExists(fso.GetDriveName(fso.GetAbsolutePathName("\\" & server & "\" & driveletter(I) & "$"))) then
Set drive = fso.Getdrive(fso.GetDriveName(fso.GetAbsolutePathName("\\" & server & "\" & driveletter(I) & "$")))
totalsize = FormatNumber(drive.TotalSize/1024/1024/1024, 1)
availsize = FormatNumber(drive.AvailableSpace/1024/1024/1024, 1)
percentfree = FormatNumber((availsize/totalsize)*100, 0)
serverusedtotal = serverusedtotal + (totalsize - availsize)
servertotalsize = servertotalsize + totalsize
serveravailsize = serveravailsize + availsize
If (percentfree < 10) AND (availsize < 1) Then
HostingCriticalReport.WriteLine ("<tr>")
HostingCriticalReport.WriteLine ("<td>" & server & "</td>")
HostingCriticalReport.WriteLine ("<td>" & driveletter(I) & "</td>")
HostingCriticalReport.WriteLine ("<td>" & totalsize & " GB</td>")
HostingCriticalReport.WriteLine ("<td>" & availsize & " GB</td>")
HostingCriticalReport.WriteLine ("<td>" & percentfree & "%</td>")
HostingCriticalReport.WriteLine ("<td>" & FormatNumber(totalsize - availsize, 1) & " GB</td>")
HostingCriticalReport.WriteLine ("</tr>")
strSendHostingEmail = "true"
End If
WebFullReport.WriteLine ("<tr>")
WebFullReport.WriteLine ("<td>" & server & "</td>")
WebFullReport.WriteLine ("<td>" & driveletter(I) & "</td>")
WebFullReport.WriteLine ("<td>" & totalsize & " GB</td>")
WebFullReport.WriteLine ("<td>" & availsize & " GB</td>")
WebFullReport.WriteLine ("<td>" & percentfree & "%</td>")
WebFullReport.WriteLine ("<td>" & FormatNumber(totalsize - availsize, 1) & " GB</td>")
WebFullReport.WriteLine ("</tr>")
end if
next
WebFullReport.WriteLine ("<tr bgcolor=silver><td colspan=2 align=right><b>Total </b></td>")
WebFullReport.WriteLine ("<td><b>" & servertotalsize & " GB</b></td>")
WebFullReport.WriteLine ("<td><b>" & serveravailsize & " GB</b></td>")
WebFullReport.WriteLine ("<td><b>" & FormatNumber((serveravailsize/servertotalsize)*100,0) & "%</b></td>")
WebFullReport.WriteLine ("<td><b>" & FormatNumber(serverusedtotal, 1) & " GB</b></td></tr>")
end if
Loop
'**********************
'* Format Report *
'**********************
WebFullReport.WriteLine ("</table>")
WebFullReport.WriteLine ("<Br><Br><h3>*Total Used Disk Space is " & usedtotal & "</h3>")
WebFullReport.WriteLine ("<h3>*Total Disk Space is " & total & "</h3>")
AppCriticalReport.WriteLine ("</table>")
HostingCriticalReport.WriteLine ("</table>")
addbottom
'Wscript.echo(server & ", " & letter & ", " & totalsize & ", " & availsize & ", " & percentfree & "%")
sub addtop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Create Top of WebFullReport Page
WebFullReport.WriteLine ("<html><head><title>Server HDD Stats</title></head><body>")
WebFullReport.WriteLine ("<div align=center><h1>Server HDD Stats as of " & date & " at " & time & "</h1>")
AppCriticalReport.WriteLine ("<html><head><title>Disk Space Report</title></head><body>")
AppCriticalReport.WriteLine ("<div align=center><h1>Disk Space Report as of " & date & " at " & time & "</h1>")
HostingCriticalReport.WriteLine ("<html><head><title>Disk Space Report</title></head><body>")
HostingCriticalReport.WriteLine ("<div align=center><h1>Disk Space Report as of " & date & " at " & time & "</h1>")
end sub
sub addbottom
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Add Bottom of Web Page
WebFullReport.WriteLine ("</div>")
WebFullReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
WebFullReport.WriteLine ("<hr>")
WebFullReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
WebFullReport.WriteLine ("")
WebFullReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
WebFullReport.WriteLine ("Last Review Date: " & date & "<br>")
WebFullReport.WriteLine ("Page Contact: tim<br>")
WebFullReport.WriteLine ("</body>")
WebFullReport.WriteLine ("</html>")
AppCriticalReport.WriteLine ("</div>")
AppCriticalReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
AppCriticalReport.WriteLine ("<hr>")
AppCriticalReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
AppCriticalReport.WriteLine ("")
AppCriticalReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
AppCriticalReport.WriteLine ("Last Review Date: " & date & "<br>")
AppCriticalReport.WriteLine ("Page Contact: tim<br>")
AppCriticalReport.WriteLine ("</body>")
AppCriticalReport.WriteLine ("</html>")
HostingCriticalReport.WriteLine ("</div>")
HostingCriticalReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
HostingCriticalReport.WriteLine ("<hr>")
HostingCriticalReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
HostingCriticalReport.WriteLine ("")
HostingCriticalReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
HostingCriticalReport.WriteLine ("Last Review Date: " & date & "<br>")
HostingCriticalReport.WriteLine ("Page Contact: tim<br>")
HostingCriticalReport.WriteLine ("</body>")
HostingCriticalReport.WriteLine ("</html>")
end sub
sub initDrives
driveletter(1) = "C"
driveletter(2) = "D"
driveletter(3) = "E"
driveletter(4) = "F"
driveletter(5) = "G"
driveletter(6) = "H"
driveletter(7) = "I"
driveletter(8) = "J"
driveletter(9) = "K"
driveletter(10) = "L"
driveletter(11) = "M"
driveletter(12) = "N"
driveletter(13) = "O"
driveletter(14) = "P"
driveletter(15) = "Q"
driveletter(16) = "R"
driveletter(17) = "S"
driveletter(18) = "T"
driveletter(19) = "U"
driveletter(20) = "V"
driveletter(21) = "W"
driveletter(22) = "X"
driveletter(23) = "Y"
driveletter(24) = "Z"
end sub
WebFullReport.close
AppCriticalReport.close
HostingCriticalReport.close
'**********************
'* Email Report *
'**********************
'Disk space email
'strEmailMsg = "Attached below are the disk space reports for " & Date & vbcrlf & vbcrlf &_
' "ACTION ITEMS:" & vbcrlf & "1. Review the attached documents." & vbcrlf &_
' "2. Perform a file cleanup on critical servers where necessary." & vbcrlf
'Set objMessage = CreateObject("CDO.Message")
'objMessage.From = "xxxxxxxx"
'objMessage.To = "xxxxxxxxxx"
'objMessage.Subject = "Disk Space Report for " & Date
'objMessage.TextBody = strEmailMsg
'objMessage.AddAttachment "C:\DiskSpace\CriticalHostingDiskReport.htm"
'objMessage.AddAttachment "C:\DiskSpace\FullDiskReport.htm"
'Authentication level for access to SMTP server
'objMessage.Configuration.Fields.Item _
'(" = 2
'Name or IP of Remote SMTP Server
'objMessage.Configuration.Fields.Item _
'(" = "xx.xx.xx.xx"
'Server port (typically 25)
'objMessage.Configuration.Fields.Item _
'(" = 25
'objMessage.Configuration.Fields.Update
'objMessage.Send
'*********************************************************************************************
'* Script name: FileServerDriveStats.vbs
'* Created on: 8/28/08
'* Last Updated:8/28/08
'* Purpose: Scan servers for disk space information
'*
'*********************************************************************************************
'*
'* Use: cscript FileServerDriveStats.vbs
'*
'* Required 3rd Party executables:
'*
'* none
'*
'* Variables to Edit:
'*
'* strServerList Path to serverlist
'* oobjMessage.From E-mail address of sender
'* objMessage.To E-mail address of receiver
'* objMessage.Subject Subject of email
'* objMessage.TextBody Message body
'* objMessage.AddAttachment Path to the attachment
'* strServerList Path to serverlist
'*
'*
'* NOTE: Users must have administrative access to target systems.
'*
'***********************************************************************************************
On Error Resume Next
'**********************
'* Standard Variables *
'**********************
strServerList = "C:\DiskSpace\serverList.txt"
'**********************
'* Constant Variables *
'**********************
Set objShell = CreateObject("Wscript.Shell")
Dim driveletter(24)
Set fso = CreateObject("Scripting.FileSystemObject")
Set WebFullReport = fso.CreateTextFile("FullDiskReport.htm", True)
Set HostingCriticalReport = fso.CreateTextFile("CriticalHostingDiskReport.htm", True)
strSendAppEmail = "false"
strSendHostingEmail = "false"
initDrives
addtop
'**********************
'* Setup HTML Table *
'**********************
WebFullReport.WriteLine ("<table border=1>")
WebFullReport.WriteLine ("<tr>")
WebFullReport.WriteLine ("<td><b>Server Name </b></td>")
WebFullReport.WriteLine ("<td><b>Drive Letter</b></td>")
WebFullReport.WriteLine ("<td><b>Total Size</b></td>")
WebFullReport.WriteLine ("<td><b>Free Space</b></td>")
WebFullReport.WriteLine ("<td><b>% Free</b></td>")
WebFullReport.WriteLine ("<td><b>Used Space</b></td>")
WebFullReport.WriteLine ("</tr>")
AppCriticalReport.WriteLine ("<table border=1>")
AppCriticalReport.WriteLine ("<tr>")
AppCriticalReport.WriteLine ("<td><b>Server Name </b></td>")
AppCriticalReport.WriteLine ("<td><b>Drive Letter</b></td>")
AppCriticalReport.WriteLine ("<td><b>Total Size</b></td>")
AppCriticalReport.WriteLine ("<td><b>Free Space</b></td>")
AppCriticalReport.WriteLine ("<td><b>% Free</b></td>")
AppCriticalReport.WriteLine ("<td><b>Used Space</b></td>")
AppCriticalReport.WriteLine ("</tr>")
HostingCriticalReport.WriteLine ("<table border=1>")
HostingCriticalReport.WriteLine ("<tr>")
HostingCriticalReport.WriteLine ("<td><b>Server Name </b></td>")
HostingCriticalReport.WriteLine ("<td><b>Drive Letter</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Total Size</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Free Space</b></td>")
HostingCriticalReport.WriteLine ("<td><b>% Free</b></td>")
HostingCriticalReport.WriteLine ("<td><b>Used Space</b></td>")
HostingCriticalReport.WriteLine ("</tr>")
'**********************
'* Scan Servers *
'**********************
Set SERVERS = fspenTextFile(strServerList, 1)
Do While SERVERS.AtEndOfStream <> True
server = SERVERS.Readline
Set objScriptExec = objShell.Exec("ping -n 1 -w 1000 " & server)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
usedtotal = usedtotal + serverusedtotal
total = total + totalsize
servertotalsize = 0
serverusedtotal = 0
serveravailsize = 0
for I=1 To 24
if fso.DriveExists(fso.GetDriveName(fso.GetAbsolutePathName("\\" & server & "\" & driveletter(I) & "$"))) then
Set drive = fso.Getdrive(fso.GetDriveName(fso.GetAbsolutePathName("\\" & server & "\" & driveletter(I) & "$")))
totalsize = FormatNumber(drive.TotalSize/1024/1024/1024, 1)
availsize = FormatNumber(drive.AvailableSpace/1024/1024/1024, 1)
percentfree = FormatNumber((availsize/totalsize)*100, 0)
serverusedtotal = serverusedtotal + (totalsize - availsize)
servertotalsize = servertotalsize + totalsize
serveravailsize = serveravailsize + availsize
If (percentfree < 10) AND (availsize < 1) Then
HostingCriticalReport.WriteLine ("<tr>")
HostingCriticalReport.WriteLine ("<td>" & server & "</td>")
HostingCriticalReport.WriteLine ("<td>" & driveletter(I) & "</td>")
HostingCriticalReport.WriteLine ("<td>" & totalsize & " GB</td>")
HostingCriticalReport.WriteLine ("<td>" & availsize & " GB</td>")
HostingCriticalReport.WriteLine ("<td>" & percentfree & "%</td>")
HostingCriticalReport.WriteLine ("<td>" & FormatNumber(totalsize - availsize, 1) & " GB</td>")
HostingCriticalReport.WriteLine ("</tr>")
strSendHostingEmail = "true"
End If
WebFullReport.WriteLine ("<tr>")
WebFullReport.WriteLine ("<td>" & server & "</td>")
WebFullReport.WriteLine ("<td>" & driveletter(I) & "</td>")
WebFullReport.WriteLine ("<td>" & totalsize & " GB</td>")
WebFullReport.WriteLine ("<td>" & availsize & " GB</td>")
WebFullReport.WriteLine ("<td>" & percentfree & "%</td>")
WebFullReport.WriteLine ("<td>" & FormatNumber(totalsize - availsize, 1) & " GB</td>")
WebFullReport.WriteLine ("</tr>")
end if
next
WebFullReport.WriteLine ("<tr bgcolor=silver><td colspan=2 align=right><b>Total </b></td>")
WebFullReport.WriteLine ("<td><b>" & servertotalsize & " GB</b></td>")
WebFullReport.WriteLine ("<td><b>" & serveravailsize & " GB</b></td>")
WebFullReport.WriteLine ("<td><b>" & FormatNumber((serveravailsize/servertotalsize)*100,0) & "%</b></td>")
WebFullReport.WriteLine ("<td><b>" & FormatNumber(serverusedtotal, 1) & " GB</b></td></tr>")
end if
Loop
'**********************
'* Format Report *
'**********************
WebFullReport.WriteLine ("</table>")
WebFullReport.WriteLine ("<Br><Br><h3>*Total Used Disk Space is " & usedtotal & "</h3>")
WebFullReport.WriteLine ("<h3>*Total Disk Space is " & total & "</h3>")
AppCriticalReport.WriteLine ("</table>")
HostingCriticalReport.WriteLine ("</table>")
addbottom
'Wscript.echo(server & ", " & letter & ", " & totalsize & ", " & availsize & ", " & percentfree & "%")
sub addtop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Create Top of WebFullReport Page
WebFullReport.WriteLine ("<html><head><title>Server HDD Stats</title></head><body>")
WebFullReport.WriteLine ("<div align=center><h1>Server HDD Stats as of " & date & " at " & time & "</h1>")
AppCriticalReport.WriteLine ("<html><head><title>Disk Space Report</title></head><body>")
AppCriticalReport.WriteLine ("<div align=center><h1>Disk Space Report as of " & date & " at " & time & "</h1>")
HostingCriticalReport.WriteLine ("<html><head><title>Disk Space Report</title></head><body>")
HostingCriticalReport.WriteLine ("<div align=center><h1>Disk Space Report as of " & date & " at " & time & "</h1>")
end sub
sub addbottom
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Add Bottom of Web Page
WebFullReport.WriteLine ("</div>")
WebFullReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
WebFullReport.WriteLine ("<hr>")
WebFullReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
WebFullReport.WriteLine ("")
WebFullReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
WebFullReport.WriteLine ("Last Review Date: " & date & "<br>")
WebFullReport.WriteLine ("Page Contact: tim<br>")
WebFullReport.WriteLine ("</body>")
WebFullReport.WriteLine ("</html>")
AppCriticalReport.WriteLine ("</div>")
AppCriticalReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
AppCriticalReport.WriteLine ("<hr>")
AppCriticalReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
AppCriticalReport.WriteLine ("")
AppCriticalReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
AppCriticalReport.WriteLine ("Last Review Date: " & date & "<br>")
AppCriticalReport.WriteLine ("Page Contact: tim<br>")
AppCriticalReport.WriteLine ("</body>")
AppCriticalReport.WriteLine ("</html>")
HostingCriticalReport.WriteLine ("</div>")
HostingCriticalReport.WriteLine ("<!----FOOTER BEGINS HERE---->")
HostingCriticalReport.WriteLine ("<hr>")
HostingCriticalReport.WriteLine ("<!--OISWEBo Not Modify or Delete This Line-->")
HostingCriticalReport.WriteLine ("")
HostingCriticalReport.WriteLine ("<h6 align=left>Review Cycle: daily<br>")
HostingCriticalReport.WriteLine ("Last Review Date: " & date & "<br>")
HostingCriticalReport.WriteLine ("Page Contact: tim<br>")
HostingCriticalReport.WriteLine ("</body>")
HostingCriticalReport.WriteLine ("</html>")
end sub
sub initDrives
driveletter(1) = "C"
driveletter(2) = "D"
driveletter(3) = "E"
driveletter(4) = "F"
driveletter(5) = "G"
driveletter(6) = "H"
driveletter(7) = "I"
driveletter(8) = "J"
driveletter(9) = "K"
driveletter(10) = "L"
driveletter(11) = "M"
driveletter(12) = "N"
driveletter(13) = "O"
driveletter(14) = "P"
driveletter(15) = "Q"
driveletter(16) = "R"
driveletter(17) = "S"
driveletter(18) = "T"
driveletter(19) = "U"
driveletter(20) = "V"
driveletter(21) = "W"
driveletter(22) = "X"
driveletter(23) = "Y"
driveletter(24) = "Z"
end sub
WebFullReport.close
AppCriticalReport.close
HostingCriticalReport.close
'**********************
'* Email Report *
'**********************
'Disk space email
'strEmailMsg = "Attached below are the disk space reports for " & Date & vbcrlf & vbcrlf &_
' "ACTION ITEMS:" & vbcrlf & "1. Review the attached documents." & vbcrlf &_
' "2. Perform a file cleanup on critical servers where necessary." & vbcrlf
'Set objMessage = CreateObject("CDO.Message")
'objMessage.From = "xxxxxxxx"
'objMessage.To = "xxxxxxxxxx"
'objMessage.Subject = "Disk Space Report for " & Date
'objMessage.TextBody = strEmailMsg
'objMessage.AddAttachment "C:\DiskSpace\CriticalHostingDiskReport.htm"
'objMessage.AddAttachment "C:\DiskSpace\FullDiskReport.htm"
'Authentication level for access to SMTP server
'objMessage.Configuration.Fields.Item _
'(" = 2
'Name or IP of Remote SMTP Server
'objMessage.Configuration.Fields.Item _
'(" = "xx.xx.xx.xx"
'Server port (typically 25)
'objMessage.Configuration.Fields.Item _
'(" = 25
'objMessage.Configuration.Fields.Update
'objMessage.Send