One of our developers was concerned today that the logs/backup drive on his SQL server was running low on space. This seemed like a perfect opportunity to throw a script together that does the following:

1. Enumerates (or singles out) each drive on the server

2. Determines the free space on those drives

3. Returns the result

This was fairly easy. All it took then was putting in some logic to only show a popup if the space was below a certain level.

Then, it had to be scheduled in Windows Scheduled Tasks to run as me when I am logged on only. (otherwise you won’t see it… and it will require you enter you password.

I have mine scheduled to run every four hours and targets our main database drives, and Exchange for free disk space. If the space is less than 1 GB, the script pops up a Message Box telling me how much free space is left on which drive.

This script currently only works against Windows 2003 servers. When that is ironed out I will post a revision.

Here it is:

‘BEGIN SCRIPT

On Error Resume Next

minfreespace=1000

If WScript.Arguments.Count = 0 Then

Wscript.Echo “Usage: freediskspace.vbs <servername>”

WScript.Quit

End If

For Each strComputer In WScript.Arguments

Set objWMIService = GetObject(”winmgmts:” _

& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)

Set colDiskDrives = objWMIService.ExecQuery _

(”Select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk where ” _

& “Name <> ‘_Total’”)

For Each objDiskDrive in colDiskDrives

IF objDiskDrive.FreeMegabytes < minfreespace THEN

Wscript.Echo strComputer & ” Drive ” & objDiskDrive.Name & ” Free Space - ” & objDiskDrive.FreeMegabytes & ” MB”

Else ‘ Wscript.Echo objDiskDrive.Name & ” has over 1000 MB free…”

End If

Next

Next

‘END SCRIPT

Enjoy

Charles Socci