Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Account Expiration Script - Notify Manager

HTVi
TV Discussion Newsgroups

Account Expiration Script - Notify Manager
vivah8t <roland.brown[ at ]gmail.com> 10/17/2008 7:03:42 PM
All,

Thanks for any help you can provide. I'm a novice at vbs scripting.
I've taken parts of existing scripts to create an Account Expiration
script. The intent is to find all accounts expiring in 14 days and
send a warning email to the Account's manager. I have all of the data
pieces of the script working and returning the appropriate data, but
I'm struggling with the piece for notifying the manager. I'm trying
to find a way to use the ouser.manager attribute which returns the CN=
name of the manager to get the manager's email address. I know there
must be a simple way to do this, but because of my inexperience I've
run into a brick wall. Below is the script. Right now I have the
notification piece turned off and just logging to a file.

Thanks,

Roland


'############################################################################
'#
'# Account Expiration Notice
'#
'# This script checks for user accounts that will exire
'# in a certian number of days and send notification email to the
manager.
'#
'############################################################################

'### VARS ###
'#

dtToday = Date

'# sScriptLogFile is the path of the log file for this script
sScriptLogFile = "C:\Scripts\AccountExpirationNotice.log"

'#
'###

'### Subs
'#

'---------------------------------------------------------------------------------------------------
' The WriteLog function writes a line to the log file and prepends the
time and date
'---------------------------------------------------------------------------------------------------
Function WriteLog(sLogLine)

sFullLogLine = now & " - " & sLogLine
oScriptLog.WriteLine (sFullLogLine)

End Function

'---------------------------------------------------------------------------------------------------
' The SendWrnEmail function sends the warning email that an account is
going to expire
'---------------------------------------------------------------------------------------------------
Function SendWrnEmail(sTo,sDisplayName,sUserName,iDays,dtExpire)

'Subject and body for the email

sWrnEmailSubject = "Account Expiration Notice - Temp / Contractor : "
& oUser.givenname & " " & oUser.sn
sWrnEmailBody = swrnEmailBody & "XXXXX" & "," & vbCrLf & vbCrLf
sWrnEmailBody = sWrnEmailBody & oUser.givenname & " " & oUser.sn &
"'s " & "account will expire in " & iDays & " days." & vbCrLf
sWrnEmailBody = sWrnEmailBody & "If you would like their access to
continue, Please submit a helpdesk ticket to EMAILADDRESSHERE and
state the duration up to 90 days." & vbCrLf & vbCrLf
sWrnEmailBody = sWrnEmailBody & "Thank You," & vbCrLf
sWrnEmailBody = sWrnEmailBody & vbCrLf
sWrnEmailBody = sWrnEmailBody & "IT Tech Support"

Set oCDOSYSMail = WScript.CreateObject("CDO.Message")
Set oCDOSYSConf = WScript.CreateObject ("CDO.Configuration")

'SMTP server's name
oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
smtpserver") = "MAIL SERVER HERE"
'Port used by the SMTP server to send e-mail
oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
smtpserverport") = 25
'SMTP over the network instead of using the local SMTP service pickup
directory
oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
sendusing") = 2
'Time-out duration
oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
smtpconnectiontimeout") = 60
oCDOSYSConf.Fields.Update

'Setting up e-mail and its configuration
Set oCDOSYSMail.Configuration = oCDOSYSConf
oCDOSYSMail.From = "FROM ADDRESS HERE"
oCDOSYSMail.To = sTo
oCDOSYSMail.Subject = sWrnEmailSubject
oCDOSYSMail.TextBody = sWrnEmailBody

'Send the email
oCDOSYSMail.Send

'Close the server mail Object
Set oCDOSYSMail = Nothing
Set oCDOSYSConf = Nothing

End Function


'---------------------------------------------------------------------------------------------------
' The CheckExpiration checks to see if the user's account is going to
expire soon, if it will then
' a notification email will be generated.
'---------------------------------------------------------------------------------------------------
Function CheckExpiration()

dtExpDate = oUser.AccountExpirationDate

'Get Days Until Account Expiration
iDays = DateDiff("d",dtToday,dtExpDate)


If iDays = -14167 Then

Else

If iDays <= 0 Then

sLogMsg = ouser.samaccountname & " --- " & "Is Already Expired"
Call WriteLog(sLogMsg)

Else

If iDays <= 14 AND > 0 Then

sLogMsg = oUser.Mail & "---" & oUser.samaccountname & " --- " & iDays
& " --- "
Call WriteLog(sLogMsg)
'Call SendWrnEmail (sTo,sDisplayName,sUserName,iDays,dtExpire)



End If
End If
End Function

'#
'###

'### Main ###
'#

Set oFSO = CreateObject("Scripting.FileSystemObject")

'Let see if the script's log file exists and if so lets open it for
appending, otherwise create and open
If oFSO.FileExists(sScriptLogFile) then
Set oScriptLog = oFSO.OpenTextFile(sScriptLogFile, 8)
Else
Set oScriptLog = oFSO.OpenTextFile(sScriptLogFile, 2, True)
End If

' Connect to LDAP and get all user accounts
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

'Lets get the correct Top level DN for our domain


strBase = "LDAP:// TOP LEVEL HERE"

strQuery = "SELECT AdsPath FROM '" & strBase & "' WHERE
objectCategory='person' and objectClass='user'"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

sLogMsg = "Connecting to LDAP"
Call WriteLog(sLogMsg)

Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
sAdsPath = objRecordSet.Fields("AdsPath")
set oUser = GetObject(sAdsPath)

Call CheckExpiration()

objRecordSet.MoveNext
err.clear
Loop
wScript.Echo "Done!"
oScriptLog.Close

objConnection.Close

'#
'###
Re: Account Expiration Script - Notify Manager
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 10/18/2008 1:19:21 AM
See my reply in microsoft.public.scripting.vbscript.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"vivah8t" <roland.brown[ at ]gmail.com> wrote in message
news:28dbfd7e-f17f-47bd-91dc-2816cfbbd2a8[ at ]a29g2000pra.googlegroups.com...
[Quoted Text]
> All,
>
> Thanks for any help you can provide. I'm a novice at vbs scripting.
> I've taken parts of existing scripts to create an Account Expiration
> script. The intent is to find all accounts expiring in 14 days and
> send a warning email to the Account's manager. I have all of the data
> pieces of the script working and returning the appropriate data, but
> I'm struggling with the piece for notifying the manager. I'm trying
> to find a way to use the ouser.manager attribute which returns the CN=
> name of the manager to get the manager's email address. I know there
> must be a simple way to do this, but because of my inexperience I've
> run into a brick wall. Below is the script. Right now I have the
> notification piece turned off and just logging to a file.
>
> Thanks,
>
> Roland
>
>
> '############################################################################
> '#
> '# Account Expiration Notice
> '#
> '# This script checks for user accounts that will exire
> '# in a certian number of days and send notification email to the
> manager.
> '#
> '############################################################################
>
> '### VARS ###
> '#
>
> dtToday = Date
>
> '# sScriptLogFile is the path of the log file for this script
> sScriptLogFile = "C:\Scripts\AccountExpirationNotice.log"
>
> '#
> '###
>
> '### Subs
> '#
>
> '---------------------------------------------------------------------------------------------------
> ' The WriteLog function writes a line to the log file and prepends the
> time and date
> '---------------------------------------------------------------------------------------------------
> Function WriteLog(sLogLine)
>
> sFullLogLine = now & " - " & sLogLine
> oScriptLog.WriteLine (sFullLogLine)
>
> End Function
>
> '---------------------------------------------------------------------------------------------------
> ' The SendWrnEmail function sends the warning email that an account is
> going to expire
> '---------------------------------------------------------------------------------------------------
> Function SendWrnEmail(sTo,sDisplayName,sUserName,iDays,dtExpire)
>
> 'Subject and body for the email
>
> sWrnEmailSubject = "Account Expiration Notice - Temp / Contractor : "
> & oUser.givenname & " " & oUser.sn
> sWrnEmailBody = swrnEmailBody & "XXXXX" & "," & vbCrLf & vbCrLf
> sWrnEmailBody = sWrnEmailBody & oUser.givenname & " " & oUser.sn &
> "'s " & "account will expire in " & iDays & " days." & vbCrLf
> sWrnEmailBody = sWrnEmailBody & "If you would like their access to
> continue, Please submit a helpdesk ticket to EMAILADDRESSHERE and
> state the duration up to 90 days." & vbCrLf & vbCrLf
> sWrnEmailBody = sWrnEmailBody & "Thank You," & vbCrLf
> sWrnEmailBody = sWrnEmailBody & vbCrLf
> sWrnEmailBody = sWrnEmailBody & "IT Tech Support"
>
> Set oCDOSYSMail = WScript.CreateObject("CDO.Message")
> Set oCDOSYSConf = WScript.CreateObject ("CDO.Configuration")
>
> 'SMTP server's name
> oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
> smtpserver") = "MAIL SERVER HERE"
> 'Port used by the SMTP server to send e-mail
> oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
> smtpserverport") = 25
> 'SMTP over the network instead of using the local SMTP service pickup
> directory
> oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
> sendusing") = 2
> 'Time-out duration
> oCDOSYSConf.Fields("http://schemas.microsoft.com/cdo/configuration/
> smtpconnectiontimeout") = 60
> oCDOSYSConf.Fields.Update
>
> 'Setting up e-mail and its configuration
> Set oCDOSYSMail.Configuration = oCDOSYSConf
> oCDOSYSMail.From = "FROM ADDRESS HERE"
> oCDOSYSMail.To = sTo
> oCDOSYSMail.Subject = sWrnEmailSubject
> oCDOSYSMail.TextBody = sWrnEmailBody
>
> 'Send the email
> oCDOSYSMail.Send
>
> 'Close the server mail Object
> Set oCDOSYSMail = Nothing
> Set oCDOSYSConf = Nothing
>
> End Function
>
>
> '---------------------------------------------------------------------------------------------------
> ' The CheckExpiration checks to see if the user's account is going to
> expire soon, if it will then
> ' a notification email will be generated.
> '---------------------------------------------------------------------------------------------------
> Function CheckExpiration()
>
> dtExpDate = oUser.AccountExpirationDate
>
> 'Get Days Until Account Expiration
> iDays = DateDiff("d",dtToday,dtExpDate)
>
>
> If iDays = -14167 Then
>
> Else
>
> If iDays <= 0 Then
>
> sLogMsg = ouser.samaccountname & " --- " & "Is Already Expired"
> Call WriteLog(sLogMsg)
>
> Else
>
> If iDays <= 14 AND > 0 Then
>
> sLogMsg = oUser.Mail & "---" & oUser.samaccountname & " --- " & iDays
> & " --- "
> Call WriteLog(sLogMsg)
> 'Call SendWrnEmail (sTo,sDisplayName,sUserName,iDays,dtExpire)
>
>
>
> End If
> End If
> End Function
>
> '#
> '###
>
> '### Main ###
> '#
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> 'Let see if the script's log file exists and if so lets open it for
> appending, otherwise create and open
> If oFSO.FileExists(sScriptLogFile) then
> Set oScriptLog = oFSO.OpenTextFile(sScriptLogFile, 8)
> Else
> Set oScriptLog = oFSO.OpenTextFile(sScriptLogFile, 2, True)
> End If
>
> ' Connect to LDAP and get all user accounts
> Set objCommand = CreateObject("ADODB.Command")
> Set objConnection = CreateObject("ADODB.Connection")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> Set objCommand.ActiveConnection = objConnection
>
> 'Lets get the correct Top level DN for our domain
>
>
> strBase = "LDAP:// TOP LEVEL HERE"
>
> strQuery = "SELECT AdsPath FROM '" & strBase & "' WHERE
> objectCategory='person' and objectClass='user'"
>
> objCommand.CommandText = strQuery
> objCommand.Properties("Page Size") = 100
> objCommand.Properties("Timeout") = 30
> objCommand.Properties("Cache Results") = False
>
> sLogMsg = "Connecting to LDAP"
> Call WriteLog(sLogMsg)
>
> Set objRecordSet = objCommand.Execute
> Do Until objRecordSet.EOF
> sAdsPath = objRecordSet.Fields("AdsPath")
> set oUser = GetObject(sAdsPath)
>
> Call CheckExpiration()
>
> objRecordSet.MoveNext
> err.clear
> Loop
> wScript.Echo "Done!"
> oScriptLog.Close
>
> objConnection.Close
>
> '#
> '###


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen