Date created: Friday, March 26, 2010 8:47:47 PM. Last modified: Thursday, December 13, 2012 11:41:20 AM
Disk Usage
I wrote this VBScript to call on a scheduled task from servers to dump the local disk drive space to a CSV file so that over time I can see the decline (or incline if something magical is happening) of free disk space to estimate when a server is going to run out, roughly!
A few things to note; swap Win32_LogicalDisk for Win32_DiskDrive if you are NOT using a hardware RAID. This script runs on Windows Server 2003 servers all running on hardware RAIDs so the storage provided to the OS is over a virtual disk volume on top of a logical disk, not a physical disk!
Also, change 'arrProperty.FreeSpace' for 'arrProperty.Size' if you just want to see disk sizes.
' Script to monitor diskusage
' Written by James Bensley (jwbensley (at) gmail.com)
' ----------------------------------------------------------------------------------------
' ----------------------------------------DECLARATIONS------------------------------------
' Make sure we don't miss any variables out
Option Explicit
' Objects
Dim objWMIService ' An object to store our Windows Managment object reference
Dim objResults ' A collection of objects returned from our management query
Dim arrProperty ' An array of properties for each returned object
Dim objFSO ' Create an object for calling file system object for our output log
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile
' Integers
Dim intArgCount ' How many args are we expecting
intArgCount = 3
Dim intL ' General integer for loop counting
' Strings
Dim strDriveLetter, strLogFile, strComputer, strLogOutput
Dim intReadMode
' ----------------------------------------------------------------------------------------
' -----------------------------------------MAIN LOOP--------------------------------------
' Check the parsed arguments
Argumentative()
' If the code returns here everything is good, lets being...
set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objResults = objWMIService.ExecQuery ("Select * From Win32_LogicalDisk") ' Query the logical disks
' We could have used Win32_DiskDrive but these are in a hardware RAID which has different properties
strLogOutput = Now() & vbNewLine ' Start the ouput log with the current date and time
For Each arrProperty in objResults
If arrProperty.DriveType = 3 Then
If strDriveLetter = "*" Then
strLogOutput = strLogOutput & "," & arrProperty.Name
strLogOutput = strLogOutput & "," & FormatNumber((arrProperty.FreeSpace / 1024^3), 2) & "GB" & vbNewLine
Else
If arrProperty.Name = strDriveLetter then
strLogOutput = strLogOutput & "," & arrProperty.Name
strLogOutput = strLogOutput & "," & FormatNumber((arrProperty.FreeSpace / 1024^3), 2) & "GB" & vbNewLine
End If
End If
End If
Next
' Are we priting this to the screen or to a CSV?
If strLogFile = "*" Then
Wscript.echo strLogOutput
Else
' Check weather are log file exists, i.e. it has been ran today
If Not objFSO.FileExists(strLogFile) Then
objFSO.CreateTextFile(strLogFile) ' If not then create it
' and set the read mode
intReadMode = 2 ' 2 is for writing
strLogOutPut = "Date,Drive,Free Space" & vbNewLine & strLogOutput
Else
intReadMode = 8 ' 8 is for appending
End If
Set objLogFile = objFSO.OpenTextFile (strLogFile, intReadMode, True)
objLogFile.Write(strLogOutPut)
objLogFile.Close
End IF
' Clean up after your self kids
set objWMIService = Nothing
set objResults = Nothing
' ----------------------------------------------------------------------------------------
' -----------------------------------------SUBROUTINES------------------------------------
Sub Argumentative()
' This sub handles the parsed arguments to the script to make sure everything is honky-dory
' Make sure we haven't been given too many arguments otherwise
' the script is going to do what the user expected
If WScript.Arguments.Count > intArgCount Then
WScript.Echo "Error: to many arguments were given"
Call PrintUsage() ' Remind the user of the usage details
WScript.Quit ' Lets exit this town!
End If
' What if not enough arguments were given?
If WScript.Arguments.Count < intArgCount Then
WScript.Echo "Error: not enough arguments were given"
Call PrintUsage() ' Remind the use of the usage details
WScript.Quit ' Lets exit this town!
End IF
On Error Resume Next
strComputer = WScript.Arguments.item(0)
strDriveLetter = WScript.Arguments.item(1)
strLogFile = WScript.Arguments.item(2)
' Did an error occur getting the arguments?
If Err.Number <> 0 Then
WScript.Echo "Error: failed to set parsed arguments"
WScript.Echo "Error Number: " & Err.Number
WScript.Echo "Error Number (Hex): " & Hex(Err.Number)
WScript.Echo "Source: " & Err.Source
WScript.Echo "Description: " & Err.Description
Call PrintUsage() ' Remind the use of the usage details
WScript.Quit ' Lets exit this town!
End If
End Sub
sub PrintUsage()
' This sub prints the usage info
' Print out the arguments parsed for aditional debugging help
Wscript.Echo vbNewLine & "Arguments given:"
For intL = 0 to WScript.Arguments.Count - 1
Wscript.Echo WScript.Arguments.Item(intL)
Next
WScript.Echo vbNewLine & "Useage details:"
WScript.Echo "<ScriptName>.vbs <Computer> <Drive Letter or *> <* (print to screen) or path_to_log.csv>"
WScript.Echo vbNewLine & "DiskStats.vbs . C: *"
WScript.Echo vbNewLine & "OR" & vbNewLine & vbNewLine & "DiskStats.vbs 2k3FS01 * C:\LogsDir\FS01_All_Drives.csv" & vbNewLine
End Sub
Previous page: SVN Notes
Next page: Empty Recycling Bin