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

Posted on 2007-08-01
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
    LVL 2

    Expert Comment

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

    Accepted Solution

    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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now