Since few days I was facing a problem that some of the servers were short of free space due to disk full.
I was looking for quick & simple way to monitor free space on server. I found some sripts on internet & modified it to suit my requirement .
Below script connects to all listed servers using WMI query , creates a report on c:\ . It can also email the report if you want.
How to use this script ?
- Copy below code in notepad
- List names of computers / server in arrServers .
- Set flag for email true / false & settings for email server
- Save file as HDDSpaceCheck.vbs file.
- Double click vbs file . It shall show report in minute or so depending on number of servers.
- You can use windows scheduled task to schedule this activity & get email notification .
‘ ******************** Start of Script *************
‘ Author : Rahul S Bagal 07/13/2009
‘ This Script Checks Free Disk space in servers defined in “arrServers “
‘ Define Names of servers to be checked for disk space
arrServers = Array(“Server1″,”Server2″,”Server3″,”Server4”)
‘ Email Settings
Const SendEmail = True ‘ Turn this flag to false you you do not wish to receive the report in email
const DisplayReportOnScreen = True ‘ Turn this flag if you do not want to open the created report file.
strServer = “MailserverName”
intPort = 25
strFrom = “[email protected]” ‘ Set from Email address for email reports
strTo = “[email protected]” ‘ Set to email address where you wish to receive email
‘ Report File defination
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
‘ This is the file name where email
strFilePath = “C:\HDDRerport” & Month ( now ) & Day(now) & year(now) & “.txt”
‘The WMI query bit…
For i = LBound(arrServers) To UBound(arrServers)
strBody = strBody & “Please find below a report on local disks for server : ” & arrServers(i) & vbCrLf & vbCrLf
On Error Resume Next
Set objWMI = GetObject(“winmgmts:\\” & arrServers(i) & “\root\cimv2”)
Set colDisks = objWMI.ExecQuery(“Select * from Win32_LogicalDisk Where DriveType=3″)’ Where DeviceID = ‘” & strDiskLetter & “‘”)
If Err.Number = 0 Then
For Each objDisk in colDisks
strBody = strBody & “Drive ” & objDisk.DeviceID & ” – ” & vbTab & “Total Size(MB): ” & bytesToMB(objDisk.Size) _
& vbTab & vbTab & “Free Space(MB): ” & bytesToMB(objDisk.FreeSpace) & vbtab & ” Free Space ( % ) : ” & round( bytesToMB(objDisk.FreeSpace) / bytesToMB(objDisk.Size) * 100 , 2 ) & vbcrlf
Next
Else
strBody = strBody & “!ERROR! connecting to ” & arrServers(i) & “. ” & Err.Number & ” – ” & Err.Description
Err.Clear
End If
strBody = strBody & vbCrLf & vbCrLf
Next
‘ Write to File
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objTextFile = objFSO.OpenTextFile _
(strFilePath , ForWriting , True)
objTextFile.WriteLine(“Server Disk Space Report” & vbCrLf )
objTextFile.WriteLine(strBody )
objTextFile.Close
set objFSO = nothing
’email Section
if SendEmail = True then
On Error Goto 0
Set objMail = CreateObject(“CDO.Message”)
With objMail
.Subject = “Server Disk Space Report”
.From = strFrom
.To = strTo
.TextBody = strBody
.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing“) = 2 ‘2 = use a specified SMTP server, 1 = use local SMTP service
.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver“) = strServer ‘Your SMTP Server
.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserverport“) = intPort ‘Server port
.Configuration.Fields.Update
.Send
End With
end if
Set objMail = Nothing
Set objWMI = Nothing
Set colDisks = Nothing
Set objNet = Nothing
‘ Display report if flag is set
if DisplayReportOnScreen = True then
Dim WshShell
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run strFilePath
Set WshShell = nothing
end if
Function bytesToMB(intbytes)
bytesToMB = FormatNumber((intbytes / 1024) / 1024,,,,-1)
End Function
‘ ******************** End of Script *************