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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-Thinking this logic for script 1

Status
Not open for further replies.

DrSeussFreak

Programmer
Feb 16, 2007
149
US
I have written a script that checks each drive letter on a server and if that drive letter has less than 10% free space left, it sends me an e-mail (per drive). My boss asked that it also send 1 e-mail a day if all the drives are ok. Below is my script, I need help to re-think the logic and was hoping for some ideas.

Thanks in advance.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName

For Each objDrive in colDrives
    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName
    
    if pFree < "10" then
        SendEmail "To goes here", "From Goes here", "Low Free Space Warning", _
                  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
'         Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
    Else
        
'         Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
    End If
Next

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP Goes Here"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
...
Dim vName
Dim bOK: bOK = True
For Each objDrive in colDrives
...
if pFree < "10" then
bOK = False
...
Next
If bOK = True Then
' send mail to the boss
End If
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This looks like it will still e-mail after each drive is checked. He wants just 1 e-mail saying all is ok.
 
This looks like it will still e-mail after each drive is checked
Really ?
Have you tried it ? If yes then what is YOUR actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Well, I said it looked, hehe, I was wrong, worked like a charm. Below is everything, thanks alot!

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName

For Each objDrive in colDrives
    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName
    
    if pFree < "10" then
        SendEmail "To", "From", "Low Free Space Warning", _
                  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
'         Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
    Else
        
'         Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
    End If
Next

'MF 10/03/2007 - Start
Dim bOK: bOK = True
For Each objDrive in colDrives
    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName
    
    if pFree < "10" then
      bOK = False
    End If
Next
		If bOK = True Then
SendEmail "To", "From", "Disk Space OK!", _
					"Life is good!"
End If
'MF 10/03/2007 - Stop

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
Why looping TWO times ?
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName
Dim bOK: bOK = True
For Each objDrive In colDrives
    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName
    If pFree < "10" Then
        bOK=False
        SendEmail "To", "From", "Low Free Space Warning", _
                  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
'         Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
    Else
'         Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
    End If
Next
If bOK = True Then
    SendEmail "To", "From", "Disk Space OK!", _
                    "Life is good!"
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Brilliant, thanks. I was actually about to consolodate, I just added it quickly to test it. Thanks mate

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName
Dim bOK: bOK = True

For Each objDrive In colDrives
    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName
    
    If pFree < "10" Then
        bOK=False
        SendEmail "To", "From", "Low Free Space Warning", _
                  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
'         Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
    Else
        
'         Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
    End If

Next

If bOK = True Then
    SendEmail "To", "From", "Disk Space OK!", _
					"I see trees of green, red roses too " & "I see them bloom for me and you " &_
					"and I think to myself what a wonderful world"
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top