Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with drive space totals 1

Status
Not open for further replies.

timeiu2009

Technical User
Mar 2, 2009
17
US
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 = fso_OpenTextFile(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&nbsp;&nbsp;&nbsp;&nbsp;</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 ("<!--OISWEB:Do 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 ("<!--OISWEB:Do 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 ("<!--OISWEB:Do 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
 
Can you be more descriptive in the way that the numbers are off?

The issue that I see "FormatNumber" function simply drops all decimal places after the first. You may be seeing rounding issues.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Here's an example of the report that I have condensed in excel. I totaled the relevant columns in excel, and you can see that the "used space" is pretty close, but the "total space" is way off. Manually adding them up, I get 5318.1, but the total in the script shows 2785.

Server Name Drive Letter Total Size Free Space % Free Used Space
Server1 74.5 3.9 5% 70.6
Server2 68.3 43.8 64% 24.5
Server3 74.5 27 36% 47.5
Server4 74.5 66 89% 8.5
Server5 74.5 64.5 87% 10
Server6 284.5 261.3 92% 23.2
Server7 169.4 148.8 88% 20.6
Server8 169.4 147.4 87% 22
Server9 68.3 61.5 90% 6.8
Server10 19.5 13.8 71% 5.7
Server11 97.8 74.2 76% 23.6
Server12 68.3 61.2 90% 7.1
Server13 546.8 456.7 84% 90.1
Server14 67.8 26 38% 41.8
Server15 68.3 56.8 83% 11.5
Server16 350.6 182.3 52% 168.3
Server17 343 176.3 51% 166.7
Server18 273.4 209.2 77% 64.2
Server19 14.7 4.4 30% 10.3
Server20 136.7 113.3 83% 23.4
Server21 9.8 4.8 49% 5
Server22 9.8 4.8 49% 5
Server23 9.8 3.8 39% 6
Server24 9.8 3.8 39% 6
Server25 74.5 68.8 92% 5.7
Server26 74.5 68.8 92% 5.7
Server27 68.3 60.6 89% 7.7
Server28 101.7 83 82% 18.7
Server29 199.4 149.6 75% 49.8
Server30 135.7 59.5 44% 76.2
Server31 16.9 11.2 66% 5.7
Server32 1478.7 694.5 47% 784.2
Server33 74.6 47.3 63% 27.3
Server34 9.8 0.8 8% 9
5318.1 3459.7 1858.4

*Total Used Disk Space is 1849.4

*Total Disk Space is 2785
 
The following lines should be below the For...Next method and servertotalsize needs to be added instead of totalsize:

Code:
[green]For I=1 To 24
    <code>
Next[/green]

usedtotal = usedtotal + serverusedtotal
total = total + [red]servertotalsize[/red]

The original code would not add the data for the last server with the arithmatic above the For...Next method. In addition, because it was adding totalsize, the final number only showed the space total of the last drive of each server.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
...honestly, I think you should be using WMI to get this information.

Win32_LogicalDisk

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
PScottC - That worked like a charm. Thanks! It's still just a little off, but I think that can be explained by the rounding issue. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top