Directory size monitoring with VBScript
OK, I don’t admit to being anything like an expert with VBScript or Windows Scripting Host but I’m learning. I’ve bought a couple of books on the subject to help me administer the network at work and automate repetitive tasks.
We have a folder on the network attached storage server that is used for archiving aged data. Our tape backup solution uses 100GB tapes. If this archiving folder goes above 100GB, the data has to be archived off on to two or more tapes which is a nightmare to administer. I’m sure you can understand how long it takes to backup 100GB, even over 1Gbps backbone but inserting tapes to do a restore, even from a backup set that spans two tapes is the biggest pain in the backside.
I need something to check the size of this archive folder and didn’t want to invest in some naff program to do it for me. I need to check it maybe once a day so I can just run a scheduled task once a day, no expenditure needed, no other resources etc.
I stumbled across a VBScript yesterday that had all the fundamental bits of code to do what I wanted it to do so I adjusted it slightly to check the folder I wanted it to check and email me if it was over a certain size or it encountered a problem. I guess the script could also be adapted to monitor server shares and availability too since if it can’t connect it will send you an email telling you so. The obvious drawback is the fact that it can’t email you telling you that the email server is down! 😉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
Dim oFSObject Dim oFSFolder Dim sMsg Dim sDrive Dim sFolderPath sFolderPath = "\\server\share" Set oFSObject = CreateObject("Scripting.FileSystemObject") '**** Create the instance of the file system object If oFSObject.FolderExists(sFolderPath) Then '**** If the folder exists Set oFSFolder = oFSObject.GetFolder(sFolderPath) '**** Get the folder object If Not oFSFolder Is Nothing Then '**** If we successfully got the folder '42949672960 = 40GB '96636764160 = 90GB '102005473280 = 95GB If oFSFolder.Size > 102005473280 Then sMsg = "This email has been sent by VBScript." & vbcrlf _ & vbcrlf & "The folder " & sFolderPath & " is greater than 95GB." & vbcrlf _ & vbcrlf & "An archive should be created within the next week to avoid exceeding the 100GB tape limit." End If Else '**** Something went wrong trying to get the folder, perhaps permissions aren't set properly, check the rights sMsg = "The folder " & sFolderPath & " could not be retrieved. Please make sure you have the appropriate network permissions and that the folder is properly shared." End If Else '**** The folder doesn't exist, it's not shared or there is network communication trouble sMsg = "The folder " & sFolderPath & " could not be retrieved. Please check the folder's path (" & sFolderPath & ") and the local area network connection." End if If Len(sMsg) > 0 Then 'MsgBox sMsg Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Size Warning (95GB)" objMessage.Sender = "myserver @mydomain.com" objMessage.From = "myserver @mydomain.com" objMessage.To = "recipient@mydomain.com" objMessage.TextBody = sMsg '==This section provides the configuration information for the remote SMTP server. '==Normally you will only change the server name or IP. objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com" 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update '==End remote SMTP server configuration section== objMessage.Send End If Set oFSFolder = Nothing Set oFSObject = Nothing |
Since tinkering with Windows XP Service Pack 2 it has come to my attention that unless another configuration item within the CDO.Message is set, it may not work and will bounce return an error.
Simply add to avoid this problem.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0