none
Notify Manager about Account Expiration Time using VBScript RRS feed

  • Question

  • After running the script, I want to notify to the manager via mail like below. one sees this at the top of the mail So I want to put the user data that's sent by email in a table:


    Username    Expiration Date

    jack.sparrow    08.12.2016 10:34:53
    jack.jason      08.12.2016 10:34:53

    Anybody a clue on how to get the text "username" and "account expiration date" in red and the name and date of the user in blue?

    Option Explicit
    
    Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
    Dim objRootDSE, strDNSDomain, strQuery, adoRecordset
    Dim dtmDate1, dtmDate2, dtmDate3, objUser, intDays, strName, strDN
    Dim lngSeconds1, str64Bit1, lngSeconds2, str64Bit2
    Dim objShell, lngBiasKey, lngBias, k
    Dim objDate, dtmExpires
    
    ' Number of days in future.
    intDays = 1
    
    ' Find date this may days in the future.
    dtmDate1 = Now()
    dtmDate2 = DateAdd("d", intDays, dtmDate1)
    dtmDate3 = DateAdd("m", 2, dtmDate1)
    
    ' Retrieve DNS name of domain.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("DefaultNamingContext")
    
    ' Obtain local Time Zone bias from machine registry.
    Set objShell = CreateObject("Wscript.Shell")
    lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
        & "TimeZoneInformation\ActiveTimeBias")
    If (UCase(TypeName(lngBiasKey)) = "LONG") Then
        lngBias = lngBiasKey
    ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
        lngBias = 0
        For k = 0 To UBound(lngBiasKey)
            lngBias = lngBias + (lngBiasKey(k) * 256^k)
        Next
    End If
    
    ' Convert the datetime values to UTC.
    dtmDate1 = DateAdd("n", lngBias, dtmDate1)
    dtmDate2 = DateAdd("n", lngBias, dtmDate2)
    dtmDate3 = DateAdd("n", lngBias, dtmDate3)
    
    ' Find number of seconds since 1/1/1601 for these dates.
    lngSeconds1 = DateDiff("s", #1/1/1601#, dtmDate1)
    lngSeconds2 = DateDiff("s", #1/1/1601#, dtmDate2)
    lngSeconds2 = DateDiff("s", #1/1/1601#, dtmDate3)
    
    ' Convert the number of seconds to a string
    ' and convert to 100-nanosecond intervals.
    str64Bit1 = CStr(lngSeconds1) & "0000000"
    str64Bit2 = CStr(lngSeconds2) & "0000000"
    
    ' Setup ADO objects.
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection
    
    ' Search entire Active Directory domain.
    strBase = "<LDAP://" & strDNSDomain & ">"
    
    ' Filter on user objects where the account expires between the
    ' dates specified.
    strFilter = "(&(objectCategory=person)(objectClass=user)" _
        & "(accountExpires>=" & str64Bit1 & ")" _
        & "(accountExpires<=" & str64Bit2 & "))"
    
    ' Comma delimited list of attribute values to retrieve.
    strAttributes = "sAMAccountName,distinguishedName,accountExpires"
    
    ' Construct the LDAP syntax query.
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    adoCommand.CommandText = strQuery
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False
    
    ' Run the query.
    Set adoRecordset = adoCommand.Execute
    
    ' Enumerate the resulting recordset.
    Do Until adoRecordset.EOF
        ' Retrieve values.
        strName = adoRecordset.Fields("sAMAccountName").Value
        strDN = adoRecordset.Fields("distinguishedName").Value
        ' Determine when account expires.
        Set objDate = adoRecordset.Fields("accountExpires").Value
        dtmExpires = Integer8Date(objDate, lngBias)
        Wscript.Echo strName & "," & strDN & "," & CStr(dtmExpires)
        'Set valid datetime value to the AccountExpirationDate method of the user object.
        Set objUser = GetObject("LDAP://" & strDN)
        objUser.AccountExpirationDate = dtmDate3
        objUser.SetInfo 
    	
    
        ' Move to the next record in the recordset.
        adoRecordset.MoveNext
    Loop
    
    ' Clean up.
    adoRecordset.Close
    adoConnection.Close
    
    Function Integer8Date(ByVal objDate, ByVal lngBias)
        ' Function to convert Integer8 (64-bit) value to a date, adjusted for
        ' local time zone bias.
        Dim lngAdjust, lngDate, lngHigh, lngLow
        lngAdjust = lngBias
        lngHigh = objDate.HighPart
        lngLow = objDate.LowPart
        ' Account for error in IADsLargeInteger property methods.
        If (lngLow < 0) Then
            lngHigh = lngHigh + 1
        End If
        If (lngHigh = 0) And (lngLow = 0) Then
            lngAdjust = 0
        End If
        lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
            + lngLow) / 600000000 - lngAdjust) / 1440
        ' Trap error if lngDate is ridiculously huge.
        On Error Resume Next
        Integer8Date = CDate(lngDate)
        If (Err.Number <> 0) Then
            On Error GoTo 0
            Integer8Date = #1/1/1601#
        End If
        On Error GoTo 0
    
    End Function

    Kind regards,

        
    • Edited by maxbox88 Saturday, October 8, 2016 5:33 AM
    Saturday, October 8, 2016 5:32 AM

Answers