By David Wiseman (Administrator)Created 03 Aug 2008, Modified 08 Nov 2009
My Rating:
Vote
Rating:
(1 votes)
Views:8263
Downloads:1085

Drive Space Report

Language:  HTA

Compatibility

Windows XP Yes Windows 2003 Yes
Windows 2000 Unknown Windows NT Unknown
Vista Yes Windows 2008 Unknown
Description

Display a drive space report for multiple computers. This HTA can check the drives of multiple computers and produce a HTML report that will highlight any drives that are low on free space. The HTA also generates a CSV version of the report.


Notes
The Drive Space Report is a GUI script that allows you to view the disk drives and free space on several computers.  Just enter the computer name or IP Address, one per line and click the "Generate Report" button to display the report.  Click the "Download Report (CSV)" button to save the report in CSV (comma separated) format.

drive space report
 
Code

Line Numbers: On  Off      Plain Text
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
    <title>WiseSoft Drive Space Report</title>
    <hta:application applicationname="WiseSoft Drive Space Report" scroll="yes" singleinstance="yes"
        windowstate="normal">
<style type="text/css">
    body
    {
        background-color: #F2F2F2;
        font: bold 10pt arial,sans-serif;
        color:#787878;
        margin:0px;
    }
    table
    {
        width: 400;
        border: 2px solid;
        border-collapse: collapse;
        border-color: #696969;
    }
    th
    {
        border: 1px dotted #111111;
        border-color: #787878;
        color: #FFFFFF;
        font: bold 12pt arial,sans-serif;
        background-color: #787878;
        text-align: left;
    }
    td
    {
        border: 1px dotted #111111;
        border-color: #787878;
        font: bold 10pt arial,sans-serif;
        color: #787878;
    }
    .neutral
    {
        background-color: #FFFFE0;
        color: #787878;
    }
    .good
    {
        background-color: green;
        color: white;
    }
    .warning
    {
        background-color: yellow;
        color: black;
    }
    .critical
    {
        background-color: red;
        color: black;
    }
    .tableHead td
    {
        padding: 5px;
        font: bold 14pt arial,sans-serif;
        background-color: #787878;
        text-align: center;
        color: white;
    }
    h1
    {
        background-color: #787878;
        color: white;
        font: bold 20pt arial,sans-serif;
        text-align: center;
    }
</style>
</head>

<script language="VBScript">

	Option Explicit
    
    ' ********** Constants **********
    const bytesToMB = 1048576
    const bytesToGB = 1073741824
    const bytesToTB = 1099511627776
	const warningLevel = 20 ' < 20% = warning (yellow)
	const criticalLevel = 10 ' < 10% = critical (red)
	const goodLevel = 50 ' > 50% = good (green)
	' **********************************

	sub GetDriveReportsHTML
		dim html, strComputer,strComputers
		
		strComputers = txtComputers.value
		
		for each strComputer in SPLIT(strComputers,chr(13))
		    strComputer = TRIM(REPLACE(strComputer,chr(10),""))
		    if strComputer <> "" then
			    html = html & GetDriveReportHTML(strComputer)
			end if
		next
		report.InnerHTML = html
	end sub

    function ConvertToDiskUnit(byval value) 
        IF (value/bytesToTb) > 1 THEN
            ConvertToDiskUnit = round(value / bytesToTB,1) & " TB"
        ELSEIF (value/bytesToGb) > 1 THEN
            ConvertToDiskUnit = round(value / bytesToGB,1) & " GB"
        ELSE
            ConvertToDiskUnit = round(value / bytesToMB,1) & " MB"
        END IF
    end function

	function GetDriveReportHTML(byval strComputer)

		Dim objWMIService, objItem, colItems
		Dim strDriveType, strDiskSize, htmlDriveReport

		ON ERROR RESUME NEXT
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
		IF Err.Number <> 0 THEN
			GetDriveReportHTML = "<span class=""critical"">Error connecting to '" & strComputer & "'</span><br/><br/>"
			Err.Clear
			ON ERROR GOTO 0
			EXIT FUNCTION
		END IF

		ON ERROR GOTO 0
		Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")

		htmlDriveReport = "<table><tr class=""tableHead""><td colspan=""5"">" & strComputer & _
				"</td></tr><tr><th>Drive</th><th>Size</th><th>Used</th><th>Free</th><th>Free(%)</th></tr>"

		For Each objItem in colItems
	
			DIM pctFreeSpace,strFreeSpace,strusedSpace, cssClass
			pctFreeSpace = round(((objItem.FreeSpace / objItem.Size) * 100),1)
			strDiskSize = ConvertToDiskUnit(objItem.Size) 
			strFreeSpace = ConvertToDiskUnit(objItem.FreeSpace)
			strUsedSpace = ConvertToDiskUnit(objItem.Size-objItem.FreeSpace)

			IF pctFreeSpace < criticalLevel THEN
				cssClass = "critical"
			ELSEIF pctFreeSpace < warningLevel THEN
				cssClass = "warning"
			ELSEIF pctFreeSpace > goodLevel THEN
				cssClass = "good"
			ELSE
				cssClass = "neutral"
			END IF

			dim strChart 
			strChart = "<div style=""width=100%;""><span style=""width=" & 100-pctFreeSpace & _
				"%;background-color:blue;""></span><span style=""width=" & pctFreeSpace & _
				"%;background-color:#FF00FF;""></span></div>"

			htmlDriveReport = htmlDriveReport & "<tr><td>" & objItem.Name & strChart & "</td><td>" & _
					strDiskSize & "</td><td>" & strUsedSpace & "</td><td>" & _
					strFreeSpace & "</td><td class=""" & cssClass & """>" &  pctFreeSpace & "%</td></tr>"

		Next
	
		htmlDriveReport = htmlDriveReport + "</table></br>"

		GetDriveReportHTML =  htmlDriveReport
	
	end function
	
	sub GetDriveReportsCSV
	    dim csv, strComputer,strComputers, path
		
		strComputers = txtComputers.value
		
		csv = "Computer,Drive,Size,Used,Free,Free(%)"
		
		for each strComputer in SPLIT(strComputers,chr(13))
		    strComputer = TRIM(REPLACE(strComputer,chr(10),""))
		    if strComputer <> "" then
		        if csv <> "" then
		            csv = csv & VbCrLf
		        end if
			    csv = csv & GetDriveReportCSV(strComputer)
			end if
		next
	    path = INPUTBOX("Enter FileName:","Enter FileName","MyDriveReport.csv")
	    if path = "" then
	        exit sub
	    end if
	    WriteTextFile csv, path
	 end sub
	
	function GetDriveReportCSV(byval strComputer)

		Dim objWMIService, objItem, colItems
		Dim strDriveType, strDiskSize, csvDriveReport

		ON ERROR RESUME NEXT
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
		IF Err.Number <> 0 THEN
			GetDriveReportCSV = strComputer & ",Error,Error,Error,Error,Error"
			Err.Clear
			ON ERROR GOTO 0
			EXIT FUNCTION
		END IF

		ON ERROR GOTO 0
		Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")

		For Each objItem in colItems
	
			DIM pctFreeSpace,strFreeSpace,strusedSpace, cssClass
			pctFreeSpace = round(((objItem.FreeSpace / objItem.Size) * 100),1)
			strDiskSize = ConvertToDiskUnit(objItem.Size) 
			strFreeSpace = ConvertToDiskUnit(objItem.FreeSpace)
			strUsedSpace = ConvertToDiskUnit(objItem.Size-objItem.FreeSpace)

			IF pctFreeSpace < criticalLevel THEN
				cssClass = "critical"
			ELSEIF pctFreeSpace < warningLevel THEN
				cssClass = "warning"
			ELSEIF pctFreeSpace > goodLevel THEN
				cssClass = "good"
			ELSE
				cssClass = "neutral"
			END IF
			
			if csvDriveReport <> "" then 
			    csvDriveReport = csvDriveReport & VbCrLf
			end if

			csvDriveReport = csvDriveReport & strComputer & "," & objItem.Name & "," & _
					strDiskSize & "," & strUsedSpace & "," & _
					strFreeSpace & "," &  pctFreeSpace & "%"

		Next
		
		GetDriveReportCSV =  csvDriveReport

	end function
	
	sub WriteTextFile(byval txt,byval path)
	    DIM objFSO, objTextFile
	        
        set objFSO = createobject("Scripting.FileSystemObject")
        
        IF objFSO.FileExists(path) THEN
            IF msgbox("Warning: file already exists! Overwrite file?",vbYesNo+vbQuestion,"Overwrite File?") = vbNo THEN
                exit sub
            END IF
        END IF

        set objTextFile = objFSO.CreateTextFile(path)

        objTextFile.Write(txt)
        objTextFile.Close
        msgbox "Created '" & path & "' Report",vbOKOnly+vbInformation
	end sub

</script>

<body>
    <h1 style="margin-bottom: 0px;">
        Drive Space Report</h1>
    <div style="text-align: right; font: bold 8pt arial,sans-serif; color: #787878;">
        By David Wiseman, <a target="_blank" href="http://www.wisesoft.co.uk">www.wisesoft.co.uk</a>
     </div><br />
    <div style="text-align: center;">
Enter names of computers (one line per computer):<br />
        <textarea name="txtComputers" rows="4" cols="50">
localhost
.
127.0.0.1
        </textarea><br />
        <input style="font-weight:bold;" onclick="GetDriveReportsHTML" type="submit" value="Generate Report"></input> 
        <input onclick="GetDriveReportsCSV" type="submit" value="Download Report (CSV)"></input>
        <br />
        <br />
    </div>
    <div style="text-align: center;" id="report">
    </div>
</body>
</html> 

 


Got a useful script? Click here to upload!


 

  Post Comment
Order By:  
User Comments
      
David Wiseman (Administrator)
United Kingdom
Posted On: 8/7/2008 3:15:02 PM

Just added a chart (progress bar) to show the used disk space.