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!

drivespace script

Status
Not open for further replies.

vbcdor

Technical User
Jan 27, 2004
39
US
Hi all,
I need some help with a script. I am trying to write a script that will send out an email when the drive freespace on a Windows server is below 10%. Although it sends out the email and lists the drive that is having a problem, it also lists 2 other internal drives (C: and D:) and are not below 10%. How do I get rid of the other 2 drives in the email?
See below:

Const Unknown = 0
Const Removable = 1
Const Fixed = 2
Const Remote = 3
Const CDROM = 4
Const RAMDisk = 5
Dim SpaceFree
'
'
' 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

'====================================================================================
' Begin main code
'====================================================================================
str = ""

set oFs = WScript.CreateObject("Scripting.FileSystemObject")
set oDrives = oFs.Drives
strComputerName = GetCurrentComputerName ' get name only once for performance reasons

for each oDrive in oDrives
Select case oDrive.DriveType
Case Fixed
str = str & strComputerName & " " & oDrive.DriveLetter & " " & Round(oDrive.FreeSpace / oDrive.TotalSize,2) & vbcrlf


If oDrive.FreeSpace / oDrive.TotalSize < .10 Then
Set objEmail = CreateObject ("CDO.Message")
DateInfo = DateInfo & Now & VbCrLf
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * FROM Win32_ComputerSystem")
For each objSysInfo in colSettings
UserName = objSysinfo.UserName
Next
objEmail.From = "server name"
objEmail.To = "my email addr "
objEmail.Subject = "Drive Space Report"
ObjEmail.Textbody = "Total drive freespace is less than 10% for the following drive(s), see below " & vbcrlf _
& str & DateInfo
objEmail.Configuration.Fields.Item (" = x
objEmail.Configuration.Fields.Item (" = "xx.xx.x.xx"
objEmail.Configuration.Fields.Item (" = xx
objEmail.Configuration.Fields.Update
objEmail.Send
End If
End Select
next
 
is it your use of

str = str + strComputer

????

this looks like any drive which is in the collection before the one which is less than 10% will get added to the 'str'

perhaps logically you should loop through the drives and add a problem drive information into memory, (array/dictionary)

then have your email sent out later on, i.e. not in the For Next loop.... if your array or dictionary object contains anything, then build your 'str' from the contents of said array/dictionary


or you could scrap all that and move your

str = str + strComputer

into the

If oDrive.FreeSpace / oDrive.TotalSize < .10

construct...that should do it :)



Select case oDrive.DriveType
Case Fixed
If oDrive.FreeSpace / oDrive.TotalSize < .10 Then
str = str & strComputerName & " " & oDrive.DriveLetter & " " & Round(oDrive.FreeSpace / oDrive.TotalSize,2) & vbcrlf

 
does your code send more than one email...i.e. one per each drive <10%

you can limit this if you go with the memory suggestion,,,
 
You wanted this ?
str = ""
For Each oDrive In oDrives
Select Case oDrive.DriveType
Case Fixed
If oDrive.FreeSpace / oDrive.TotalSize < 0.1 Then
str = str & strComputerName & " " & oDrive.DriveLetter & " " & Round(oDrive.FreeSpace / oDrive.TotalSize, 2) & vbCrLf
End If
End Select
Next
If str <> "" Then
Set objEmail = CreateObject("CDO.Message")
DateInfo = DateInfo & Now & vbCrLf
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * FROM Win32_ComputerSystem")
For Each objSysinfo In colSettings
UserName = objSysinfo.UserName
Next
objEmail.From = "server name"
objEmail.To = "my email addr "
objEmail.Subject = "Drive Space Report"
objEmail.Textbody = "Total drive freespace is less than 10% for the following drive(s), see below " & vbCrLf _
& str & DateInfo
objEmail.Configuration.Fields.Item(" = x
objEmail.Configuration.Fields.Item(" = "xx.xx.x.xx"
objEmail.Configuration.Fields.Item(" = xx
objEmail.Configuration.Fields.Update
objEmail.Send
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
much better.
thanks mrmovie
I believe it sends just one email with all the drives listed. I will retest and see.
thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top