Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript monitor hard drive space and send an email 1

Status
Not open for further replies.

alexeybostonma

IS-IT--Management
Apr 26, 2009
8
US
Hi Folks,

I use the VBScript below to determine which server/servers gets low on drive space and get notified via email, but the issue is when it treats a CD/DVD drive as a hard drive and adds it to the list saying drive E (which is a CD drive) has 0 % left. Any suggestions? Here is the script

Const AlertHigh = .1 'This is the upper percentage of free space for an alert; 10% = .1
Const emailFrom = "email address" 'Enter the from address the email will appear from
Const emailTo = "email address" 'Enter the to address which the email will be sent to
Const ExchangeServer = "email server name" 'Enter your Exchange server name here
Const WaitTimeInMinutes = 10 'Wait time between polls measured in minutes

Dim strMessage
Dim arrServerList

arrServerList = array("server name") 'Enter the list of servers to monitor

Do until i = 2
'Clear the message variable
strMessage = ""

'Poll the array of servers
PollServers(arrServerList)

'Email if there is a message
if strMessage <> "" then
EmailAlert(strMessage)
end if

'Wait for set number of minutes then loop
WScript.Sleep(WaitTimeInMinutes*60000)

'Uncomment next line to test a few loops and quit
'i = i + 1

Loop

Sub PollServers(arrServers)
on error resume next
for each Server in arrServers
set objSvc = GetObject("winmgmts:{impersonationLevel=impersonate}//" & Server & "/root/cimv2")
set objRet = objSvc.InstancesOf("win32_LogicalDisk")
for each item in objRet
if item.DriveType = 7 then
end if
if item.FreeSpace/item.size <= AlertHigh then
strMessage = strMessage & UCase(Server) & ": Alert, drive '" & item.caption & "' is low on HDD space! There are " & FormatNumber((item.FreeSpace/1024000),0) & " MB free <7%" & vbCRLF
end if
next
next
set objSvc = Nothing
set objRet = Nothing
End Sub

Sub EmailAlert(Message)
on error resume next
Set objMessage = CreateObject("CDO.Message")
with objMessage
.From = emailFrom
.To = emailTo
.Subject = "Low Disk Space Update"
.TextBody = Message
.Configuration.Fields.Item (" = 2
.Configuration.Fields.Item (" = ExchangeServer
.Configuration.Fields.Item (" = 25
.Configuration.Fields.Update
.Send
end with
Set objMessage = Nothing
End Sub
 
set objSvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set objLogicalDisks = objSvc.InstancesOf("win32_LogicalDisk")

for each objDisk in objLogicalDisks
if (objDisk.Description = "CD-ROM Disc") then msgbox objDisk.Caption & " is a CD-ROM Drive!"
next

-Geates
 
Thanks Geates! I was wondering if I could modify the cript so it ignores CD-rom drive in the list of drive space.
 
It's always fun to see different ways of doing something. My version queries all drives then only looks at local hard disks.
Code:
'----- Connect to computer and query drives
set WMIService = GetObject("winmgmts:\\" & computerName & "\root\CIMV2") 
set driveList = WMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk",,48) 

'----- Loop through all drives and log information
For Each drive in driveList
	If (drive.DriveType = 3) Then
		If drive.Name = driveName Then
			freeGB = drive.FreeSpace/(1024^3)
			If freeGB < threshold Then
				strMessage = "Server: " & computerName & VBCRLF & "Drive: " & driveName & VBCRLF & "Free Space: " & FormatNumber(freeGB, 2) & " GB"
				Call SMTPSend("groiblesnitz.blickfleep@moronmail.com", "Disk space threshold alert", strMessage)
			End If
		End If
	End If
Next
[/i]
(driveName and threshold are command line parameters. This lets me have different thresholds for each disk.)

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Sure, you can also discover disk type using the property .DriveType - as MasterRacker did.

0 Unknown
1 No Root Directory
2 Removable Disk
3 Local Disk
4 Network Drive
5 Compact Disc
6 RAM Disk

Code:
if (objDisk.DriveType = 3) then
   'Poll the disk
else
   'disregard disk.
end if

-Geates

 
next
set objSvc = Nothing
set objRet = Nothing

move the set nothings inside the next statement :)

you could speed things up by having your disk check and email as a seperate script then all your looping / waiting script to execute say 10 shells at the most etc.

or look up a nice post by dilanttee about client server messaging stuff....or chuck a permie wmi event subscription on your target machines and have them send an email out when required and log centrally...but i guess that would be called MOM
 
Thanks for the help, althought it still adds CD-ROM to the report. Strange
 
Seriously :(

This code worked for me.

arrServers = array(".")

for each Server in arrServers
set objSvc = GetObject("winmgmts:{impersonationLevel=impersonate}//" & Server & "/root/cimv2")
set objRet = objSvc.InstancesOf("win32_LogicalDisk")

for each item in objRet
if (item.DriveType = 3) then
if ((item.FreeSpace/item.size) <= AlertHigh) then
strMessage = strMessage & UCase(Server) & ": Alert, drive '" & item.caption & "' is low on HDD space! There are " & FormatNumber((item.FreeSpace/(1024^3)), 0) & " MB free (" & formatNumber((item.FreeSpace/item.size) * 100, 0) & "%)" & vbCRLF
end if
end if
next

set objSvc = Nothing
set objRet = Nothing
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top