Change User Account Expiration to Never

Discussion in 'Scripting' started by Andreas B, Mar 7, 2008.

  1. Andreas B

    Andreas B Guest

    How do I create a VBScript to open a text file, containing user accounts that
    should get changed Account expiration to Never=
    Andreas B, Mar 7, 2008
    1. Advertisements

  2. You set the account to never expire by assigning -1 to the accountExpires
    attribute. If the file contains the NT names (pre-Windows 2000 logon names)
    of users, you can use the NameTranslate object to convert to the
    Distinguished Name. For example:
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1779 = 1
    Const ForReading = 1

    ' Specify the text file of user names.
    strFilePath = "c:\MyFolder\UserList.txt"

    ' Open the file for read access.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

    ' Determine DNS domain name from RootDSE object.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use the NameTranslate object to find the NetBIOS domain name
    ' from the DNS domain name.
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
    strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
    ' Remove trailing backslash.
    strNetBIOSDomain = Left(strNetBIOSDomain, _
    Len(strNetBIOSDomain) - 1)

    ' Read the text file.
    Do Until objFile.AtEndOfStream
    strUserNTName = Trim(objFile.ReadLine)
    ' Skip blank lines.
    If (strUserNTName <> "") Then
    ' Use NameTranslate to convert NT name to Distinguished Name.
    On Error Resume Next
    objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" &
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "User " & strUserNTName _
    & " not found in Active Directory"
    End If
    On Error GoTo 0
    strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

    ' Bind to the user object.
    Set objUser = GetObject("LDAP://" & strUserDN)

    ' Make account never expire.
    objUser.accountExpires = -1
    End If

    ' Clean up.
    Richard Mueller [MVP], Mar 7, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.