• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Help with a VBScript

I have this script and I was wondering how I could make it so that when executed it would only send an email when the account was disabled and not when the account is already disabled.  See my plans are to have this ran everyday through a scheduler and i dont want to get an email every day if the account is already disabled.  I only want to know when it was disabled if it was previously enabled.
strUserDN = "CN=test,OU=Test Accounts,OU=Security,DC=inc,DC=com"
Set objUser = GetObject("LDAP://" & strUserDN)
objUser.AccountDisabled = True
objUser.SetInfo

' Email variables:
strServer = "smtp.inc.com"
strTo = "test@test.com"
strFrom = "AcctControlAdmin@test.com"
strSubject = "Account was disabled."
strBody = "The Account was disabled." & VbCrLf

SendEmail strServer, strTo, strFrom, strSubject, strBody, ""

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        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") = strServer
        '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
        Set objMessage = Nothing
End Sub

Open in new window

0
seaninman
Asked:
seaninman
2 Solutions
 
Tony MassaCommented:
Since you're disabling the account in the script, we'll just add a couple lines to notify ONLY when the account is enabled...otherwise, it's always supposed to be disabled.

 
strUserDN = "CN=test,OU=Test Accounts,OU=Security,DC=inc,DC=com"
Set objUser = GetObject("LDAP://" & strUserDN)

'Added Code ---------------------------
If objUser.AccountDisabled = True Then
	'Do Nothing
Else
'End Added Code -----------------------

  objUser.AccountDisabled = True
  objUser.SetInfo

  ' Email variables:
  strServer = "smtp.inc.com"
  strTo = "test@test.com"
  strFrom = "AcctControlAdmin@test.com"
  strSubject = "Account was disabled."
  strBody = "The Account was disabled." & VbCrLf

  SendEmail strServer, strTo, strFrom, strSubject, strBody, ""
End If
Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        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") = strServer
        '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
        Set objMessage = Nothing
End Sub

Open in new window

0
 
seaninmanAuthor Commented:
Getting this error message after adding that... error
0
 
RobSampsonCommented:
Did you copy the entire code above?  You should have the End Sub at the bottom of your code.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now