script to query AD and actively email a responsible party that a user account is about to expire

Posted on 2007-08-01
Medium Priority
Last Modified: 2008-06-28
does anyone know of a free script, or a fee-based product to query AD and actively email a responsible party that a user account is about to expire in x number of days?

Goal is to be able to proactively notify dept managers that their contractor's user account is about to expire beofre the user gets locked out and and they have to contact IT to re-enable it.  we set all contractor accounts to expire on or about the last day of the contractors work agreement.  these typically get extended, but the hiring manager does not notify IT, therefore the account expires and has to be re-enabled for another 30 period, for example.
Question by:paulweb

Expert Comment

ID: 19611305
Wouldn't that be a job for a group policy object?
LVL 12

Accepted Solution

zoofan earned 500 total points
ID: 19611691
Here is the script to query active directory

Option Explicit
Dim strUserName, strCrDate, objAcctExp, CrUser
Dim strCrDomain, strAcctExp, strSysInfo
Dim UserINfo, strDateDiff

'Get currently logged on user information
set strSysInfo = CreateObject("ADSystemInfo")
Set CrUser = GetObject("LDAP://" & strSysInfo.Username & "")

'Assign the current date to the variable "strCrDate"
strCrDate = Date()

'Get current user's Account Expiration Date
strAcctExp = CrUser.AccountExpirationDate

'Comparing the account expiration date and the current Date
strDateDiff = datediff("d", strCrDate, strAcctExp)

'Calculations used to determine if the account will expire soon
If strDateDiff <= 5 Then 'Set it to 5 days to set off the alarm. Could be anything.
MsgBox "Your account will expire in " & strDateDiff & " days. Please contact your system administrator for help."
ElseIf strDateDiff > 5 Then
End If

From there the script to send the email, which you can call instead of the msgbox if account expires in X days



Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question