none
Assitance with vbscript which deletes a user from an OU in Active Directory that matches the employeeNumber attribute from a text file

    Question

  • Hello All,

    I'm in need of some assistance with a script that I'm working on. What I'm trying to do is delete users from an OU in Active Directory that matches the employeeNumber attribute is listed in a text file. Scripting is not my strong point and any help would be greatly appreciated. This is what I have:

    Option Explicit
    Const ForReading = 1, ForWriting = 2
    Dim strOU, objOU, objFSO, objUser, arrDelRead, objDelUserFile
    Dim strAttributeName, strAttributeValue, strValue, objAttributeFile, strAtt, strDelRead

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    strOU = "OU=Stale Users,DC=ABC,DC=LOCAL"

    strAttributeName = "employeeNumber"
    strAttributeValue = "C:\script\AD - Stale Users Delete\EmployeeNumber.txt"


    Set objAttributeFile = objFSO.OpenTextFile(strAttributeValue,ForReading)
        strDelRead = objAttributeFile.ReadAll
    objAttributeFile.Close
    arrDelRead = Split(strDelRead,VbCrLf)
    strAtt = arrDelRead(0)

    Set objOU = GetObject("LDAP://" & strOU)

    objOU.Filter = Array("user")

    Set objDelUserFile = objFSO.OpenTextFile(strAttributeValue, ForReading)

    For Each objUser In objOU
        If (objUser.Class = "user") Then
            On Error Resume Next
            strValue = ""
            strValue = objUser.Get(strAttributeName)
            On Error GoTo 0
              If strValue  = strAtt Then
            On Error GoTo 0
            'objUser.DeleteObject (0)
            MsgBox "User object " & strValue & " with employee number, " & strAtt & ", deleted" 'For testing
        Else
            MsgBox "User object " & strValue & " with employee number, " & strAtt & ", not deleted" 'For testing
            End If
        End If
    Next

    Thursday, February 07, 2013 6:56 PM

Answers

  • Sorry - It has been a while since I did that. I hate deleteing thisngs so I didn't fuly test.

    Here is one that is tested and I did rememebr that it is the relative name.

    Const DOMAIN = "LDAP://dc=mydomian,dc=com"
    Const EMPLOYEE_FILE = "C:\script\AD - Stale Users Delete\EmployeeNumber.txt"
     
    Set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
     
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(EMPLOYEE_FILE)
    While Not file.AtEndOfStream
        
        strEmployeeNumber = file.ReadLine()
     
        Set rs = conn.Execute( "SELECT aDSPath FROM '" & DOMAIN & "' WHERE EmployeeNumber='" & strEmployeeNumber & "'" )
        While Not rs.EOF
            Set user = GetObject(rs.Fields("aDSPath").Value)
            MsgBox user.CN
            Set OU = GetObject(user.Parent)
            'ou.Delete "user", "CN=" & user.CN
            rs.MoveNext
        Wend
            
    Wend
     
    file.Close

    This one should work and I will replace teh other bad ones.


    ¯\_(ツ)_/¯

    • Marked as answer by PN92683 Thursday, February 07, 2013 9:52 PM
    Thursday, February 07, 2013 9:48 PM

All replies

  • Here is a much cleaner and more direct way to do this.  It does an absolute lookup of the employee

    Const DOMAIN = "LDAP://dc=mydomian,dc=com"
    Const EMPLOYEE_FILE = "C:\script\AD - Stale Users Delete\EmployeeNumber.txt"
     
    Set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
     
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(EMPLOYEE_FILE)
    While Not file.AtEndOfStream
        
        strEmployeeNumber = file.ReadLine()
     
        Set rs = conn.Execute( "SELECT aDSPath FROM '" & DOMAIN & "' WHERE EmployeeNumber='" & strEmployeeNumber & "'" )
        While Not rs.EOF
            Set user = GetObject(rs.Fields("aDSPath").Value)
            MsgBox user.CN
            Set OU = GetObject(user.Parent)
            'ou.Delete "user", "CN=" & user.CN
            rs.MoveNext
        Wend
            
    Wend
     
    file.Close

    ¯\_(ツ)_/¯




    • Edited by jrv Thursday, February 07, 2013 9:48 PM Fixed code.
    Thursday, February 07, 2013 7:55 PM
  • Sorry - I messed up the last post but it is fixed.

    The script can be run against any employeenuimbers anywhere in AD.  Employee numbers should be unique. The search can also be confind to a single OU if the numbers are not unique. for some reason. The ADO search is the fastest and most reliable way to retrieve these items.  Ther e is no need to constantly enumerate the contents of an OU.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, February 07, 2013 8:13 PM
    Thursday, February 07, 2013 8:07 PM
  • Hi JRV,

    Thank you so much the quick response. Your script is a lot cleaner than what I had.

    I'm getting this error when I run the script with the delete command:

    Error: There is no such object on the server.

    Code: 80072030

    Any ideas? Thanks

    Thursday, February 07, 2013 9:27 PM
  • Yes I did
    Thursday, February 07, 2013 9:41 PM
  • Sorry - It has been a while since I did that. I hate deleteing thisngs so I didn't fuly test.

    Here is one that is tested and I did rememebr that it is the relative name.

    Const DOMAIN = "LDAP://dc=mydomian,dc=com"
    Const EMPLOYEE_FILE = "C:\script\AD - Stale Users Delete\EmployeeNumber.txt"
     
    Set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
     
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(EMPLOYEE_FILE)
    While Not file.AtEndOfStream
        
        strEmployeeNumber = file.ReadLine()
     
        Set rs = conn.Execute( "SELECT aDSPath FROM '" & DOMAIN & "' WHERE EmployeeNumber='" & strEmployeeNumber & "'" )
        While Not rs.EOF
            Set user = GetObject(rs.Fields("aDSPath").Value)
            MsgBox user.CN
            Set OU = GetObject(user.Parent)
            'ou.Delete "user", "CN=" & user.CN
            rs.MoveNext
        Wend
            
    Wend
     
    file.Close

    This one should work and I will replace teh other bad ones.


    ¯\_(ツ)_/¯

    • Marked as answer by PN92683 Thursday, February 07, 2013 9:52 PM
    Thursday, February 07, 2013 9:48 PM
  • That worked, you're awesome! No need to apologize, I really appreciate your time and effort. Thanks again and have a great day!
    Thursday, February 07, 2013 9:54 PM