This is the first VB script I've done in a long time - but I think it's probably pretty handy for some people so here it is - be kind :-)
It is a simple script which modified from an original one from http://www.adiscon.com/ which checks all the fixed disks on a windows machine and can email the results.
The modified script is slightly more abstracted and doesn't require any additional components to send the email. It should be useful for monitoring disk space on DB servers or web servers that have log files on busy websites.
It is split into two parts - the library which holds all the functions and the config main code.
lib_diskspace.vbs
This is the library of functions
' Constants for drive types
Const Unknown = 0
Const Removable = 1
Const Fixed = 2
Const Remote = 3
Const CDROM = 4
Const RAMDisk = 5
' Send a mail message
Sub SendMail(Sender, Recipient, Subject, Message,Server,Port)
Set objEmail = CreateObject(
"CDO.Message")
objEmail.From = Sender
objEmail.To = Recipient
objEmail.Subject = Subject
objEmail.Textbody = Message
objEmail.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserver") = Server
objEmail.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
objEmail.Configuration.Fields.Update()
objEmail.Send
End Sub
' get current computer name (from system environment variables)
Function GetCurrentComputerName
set oWsh = WScript.CreateObject(
"WScript.Shell")
set oWshSysEnv = oWsh.Environment(
"PROCESS")
GetCurrentComputerName = oWshSysEnv(
"COMPUTERNAME")
End Function
' get free space report
Function GetFreeSpaceReport
set oFs = WScript.CreateObject(
"Scripting.FileSystemObject")
set oDrives = oFs.Drives
for each oDrive in oDrives
Select case oDrive.DriveType
Case Fixed
GetFreeSpaceReport = GetFreeSpaceReport & oDrive.DriveLetter &
": " & Round(oDrive.FreeSpace/(
1024*1024)) &
"MB free (" & Round(
100 * (oDrive.FreeSpace/oDrive.TotalSize),
2) &
"%)" & vbcrlf
End Select
next
End Function
diskspace.wsf
This is the main script that should be edited to your configuration, and added to your scheduled tasks.
<job>
<script src="lib_diskspace.vbs" language="vbscript" /> <script language="vbscript"> '====================================================================================
' Begin main code
'====================================================================================
' general constants
Const MailServer =
"192.168.166.1" ' Mail Server to use for SMTP
Const MailServerPort =
"25" ' SMTP Port used at Mail server (
25 is default)
Const MailTo =
"mark@lynchconsulting.com.au" ' Who should be notified
Const MailFrom =
"server@lynchconsulting.com.au" str =
"" subject =
"" strComputerName = GetCurrentComputerName ' get name only once for performance reasons
subject =
"Drive Space Report " & strComputerName
str = str & strComputerName & vbcrlf
str = str & GetFreeSpaceReport
'Output to screen - useful for debugging
Msgbox str
'Send mail to administrator
SendMail MailFrom , MailTo , subject , str, MailServer , MailServerPort
</script> </job>
Hope it helps.