Jump to content

Active Directory - Need to search user using Full Name


MozQue
 Share

Recommended Posts

Hello,

 

Need assistance on my work. I am trying to get the user's object properties as well as their manager object properties. All is working fine when the manager properties contains ID. But some of the user got the Name of their manager instead of the ID.

image.png.68bed653e695a1452e1184cba5ba7aed.png

And that is when I need to search the manager's properties by their Name.

Below is the code of my work I'm working at. Any help will be greatly appreciated.

 

#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
#include <AD.au3>
#include <Array.au3>


; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
Global $aProperties[1][2]
Global $user = InputBox("Get user's manager","Input user ID: ")

$user_exist = _AD_ObjectExists($user,"")


if $user_exist = 0 Then
   MsgBox(0,"","User not found")
   Else
      $aUserProperties = _AD_GetObjectProperties($user)
      _ArrayDisplay($aUserProperties)
      $aUserProperties = _AD_GetObjectProperties($user,"cn,displayName,givenName,sn,mail,ComcastContractorID")

      ;user properties
      $NTID = _ArrayToString($aUserProperties, "",1,1,"",1,1)
      $FirstName = _ArrayToString($aUserProperties, "",4,4,"",1,1)
      $LastName = _ArrayToString($aUserProperties, "",6,6,"",1,1)
      $UserEmail = _ArrayToString($aUserProperties, "",5,5,"",1,1)
      $UserContractorID = _ArrayToString($aUserProperties, "",2,2,"",1,1)

      ;get manager info
      $aProperties = _AD_GetObjectProperties($user,"manager")
      $manager = _ArrayToString($aProperties,"",1,1,"",1,1)
      $manager2 = StringSplit($manager, ",")
      $manager3 = _ArrayToString($manager2,"",1,1,"",0,0)
      $manager4 = StringSplit($manager3, "=")
      $manager5 = _ArrayToString($manager4,"",2,2,"",0,0)

      ;manager properties
      $final_manager = _AD_GetObjectProperties($manager5,"cn,givenName,sn,mail,telephoneNumber")
      $final_manager_fname = _ArrayToString($final_manager, "",2,2,"",1,1)
      $final_manager_lname = _ArrayToString($final_manager, "",4,4,"",1,1)
      $final_manager_email = _ArrayToString($final_manager, "",3,3,"",1,1)
      $final_manager_telnum = _ArrayToString($final_manager, "",5,5,"",1,1)

      ;display user and manager details
      MsgBox(0,"CONTRACTOR DETAILS","USER INFO:" & @LF & "NT ID: " & $NTID & @LF & "Full Name: " & $FirstName & " " & $LastName & @LF & "Email Address: " & $UserEmail & @LF & "CPERNR: " & $UserContractorID & @LF & @LF & "USERS MANAGER INFO" & @LF & "Name: " & $final_manager_fname & " " & $final_manager_lname & @LF & "Email Address: " & $final_manager_email & @LF & "Telephone #: " & $final_manager_telnum)
EndIf
;_ArrayDisplay($final_manager)
_AD_Close()

Link to comment
Share on other sites

You can use something like:

#include <AD.au3>
#include <Array.au3>
#include <String.au3>

;~ Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
Global $aProperties, $sUserNTID, $sUserFirstName, $sUserLastName, $sUserEmail, $sUserContractorID, $sMngrFQDN, $aMngrProperties, $sMngrFirstName, $sMngrLastName, $sMngrEmail, $sMngrPhone
Global $sUserName = InputBox("Get user's manager","Input user ID: ")
Global $bUserName = _AD_ObjectExists($sUserName,"")
If $bUserName = 0 Then
    MsgBox(0,"","User not found: " & $sUserName)
Else
    $aUserProperties = _AD_GetObjectProperties($sUserName,"cn,displayName,givenName,sn,mail,ComcastContractorID,Manager")
    If @error Then
        MsgBox(4096, "_AD_GetObjectProperties Error", "Error getting _AD_GetObjectProperties for " & $sUserEmail)
        _AD_Close()
    EndIf
    For $i = 1 To $aUserProperties[0][0]
        Switch $aUserProperties[$i][0]
            Case "cn"
                $sUserNTID = $aUserProperties[$i][1]
            Case "givenName"
                $sUserFirstName = $aUserProperties[$i][1]
            Case "sn"
                $sUserLastName = $aUserProperties[$i][1]
            Case "mail"
                $sUserEmail = $aUserProperties[$i][1]
            Case "manager"
                $sMngrFQDN = _AD_FQDNToSamAccountName($aUserProperties[$i][1])
                    If @error Then ContinueLoop
                $aMngrProperties = _AD_GetObjectProperties($sMngrFQDN, "givenName,sn,mail,telephoneNumber")
                    If @error Then ContinueLoop
                For $j = 1 To $aMngrProperties[0][0]
                    Switch $aMngrProperties[$j][0]
                        Case "givenName"
                            $sMngrFirstName = $aMngrProperties[$j][1]
                        Case "sn"
                            $sMngrLastName = $aMngrProperties[$j][1]
                        Case "mail"
                            $sMngrEmail = $aMngrProperties[$j][1]
                        Case "telephoneNumber"
                            $sMngrPhone = $aMngrProperties[$j][1]
                    EndSwitch
                Next
            Case "ComcastContractorID"
                $sUserContractorID = $aUserProperties[$i][1]
        EndSwitch
    Next
    ;~  Display user and manager details
      MsgBox(0,"CONTRACTOR DETAILS","USER INFO:" & @LF & "NT ID: " & $sUserNTID & @LF & "Full Name: " & $sUserFirstName & " " & $sUserLastName & @LF & "Email Address: " & $sUserEmail & @LF & "CPERNR: " & $sUserContractorID & @LF & @LF & "USERS MANAGER INFO" & @LF & "Name: " & $sMngrFirstName & " " & $sMngrLastName & @LF & "Email Address: " & $sMngrEmail & @LF & "Telephone #: " & $sMngrPhone)
EndIf
_AD_Close()

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...