Avatar of gilib
gilib

asked on 

Script to email user when their user account nearly expired

Hi Guys

I need help . I need to create script that will email user when their user account nearly expiered . At the moment I go the folowing script that I got from a website

'***********************************************
'*         Connect to AD                       *
'***********************************************
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
  "SELECT AdsPath FROM 'LDAP://DC=live,DC=local' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'***********************************************
'* Search User accounts for accounts that      *
'* expire within the date range                       *
'***********************************************
Do Until objRecordSet.EOF
   Set objUser = GetObject(objRecordSet.Fields("AdsPath").Value)
        If objUser.AccountExpirationDate > strBeginDate and objUser.AccountExpirationDate < strEndDate Then
              Wscript.Echo objUser.DisplayName & "  expires soon"
            Wscript.Echo objUser.DisplayName & "    " & objUser.AccountExpirationDate
        End If    
  objRecordSet.MoveNext
Loop
Wscript.Echo "Done with searching AD"



Hope I hear from  you guys soon
VB Script

Avatar of undefined
Last Comment
gilib
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gilib
gilib

ASKER

Hey  Chris

Sorry for the late response . I been busy and holidays . I put together the script that I attached and the script that you provided . I will test it in several days and then I got back to you .   See below for the whole script . Please feel free to have a look at it and change it .


'**********************************************************************************
'*This script will search for user account that nearly expired and email the user *
'**********************************************************************************

'***********************************************
'*         Connect to AD                       *
'***********************************************
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
  "SELECT AdsPath FROM 'LDAP://DC=live,DC=local' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

'***********************************************
'* Search User accounts for accounts that      *
'* expire within the date range                *
'***********************************************
Do Until objRecordSet.EOF
   Set objUser = GetObject(objRecordSet.Fields("AdsPath").Value)
        If objUser.AccountExpirationDate > strBeginDate and objUser.AccountExpirationDate < strEndDate Then
            SendMail "objUser.EmailAddress", "Account Expiration Warning", "Please contact your administrator"

            'Wscript.Echo objUser.DisplayName & "  expires soon"
            'Wscript.Echo objUser.DisplayName & "    " & objUser.AccountExpirationDate
        End If    
  objRecordSet.MoveNext
Loop



'**********************************************
'*    Procedure to send email to using the    *
'*    company smtp address                    *
'**********************************************
Sub SendMail(strTo, strSubject, strBody)
      Const MAIL_SERVER = "smtp.tns-global.com"
 
      Dim objMail
 
      Set objMail = CreateObject("CDO.Message")
      objMail.Subject = strSubject
      objMail.From = "account.expiration@yourdomain.com"
      objMail.To = strTo
      objMail.TextBody = strBody
 
      objMail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      objMail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
            = MAIL_SERVER
      objMail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
            = 25
 
      objMail.Configuration.Fields.Update
      objMail.Send
      Set objMail = Nothing
End Sub
 
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo