Active Directory (Domain): Anfragen ob User in der AD existiert

  • Wie kann ich im AD Abfragen, ob ein User existiert?

    Mir würde schon etwas ganz simples reichen:

    [autoit]


    If not $MyUser in Domain then
    Exit (44)
    EndIF

    [/autoit]

    Leider übersteigt das ActiveDirectory UDF meinen Scriptingskill deutlich ;(

    Kann mir jemand unter die Arme greifen?

    Danke euch!

    Surfy

    Einmal editiert, zuletzt von Surfy (4. Oktober 2012 um 15:01)

  • Schau Dir mal main Active Directory UDF an (Download siehe meine Signatur). Die Funktion _AD_ObjectExists sollte tun was Du brauchst.

  • Dank der ausführlichen Doku zum UDF denke ich dass ich mit dem hier zurande komme:

    Spoiler anzeigen
    [autoit]

    #include <AD.au3>
    ; Open Connection to the Active Directory
    _AD_Open()
    ; *****************************************************************************
    ; Example 1 - Process the current user.
    ; In the first step get some attributes of the current user.
    ; In the second step check the user for this attributes.
    ; *****************************************************************************
    ; Get some information for the current logged on user we later can test
    Global $asProperties[8] = ["sAMAccountName", "cn", "mail", "userPrincipalName", "name", "mailNickname", "displayName", "dNSHostName"]
    Global $asObjects[8], $iCount
    For $iCount = 0 To UBound($asObjects) - 1
    $asObjects[$iCount] = _AD_GetObjectAttribute(@UserName, $asProperties[$iCount])
    Next
    ; Test for the information we gathered above
    Global $sOutput = "Get Attributes for User: " & @UserName & @CRLF & @CRLF
    For $iCount = 0 To UBound($asObjects) - 1
    If _AD_ObjectExists($asObjects[$iCount], $asProperties[$iCount]) Then
    $sOutput &= "Object '" & $asObjects[$iCount] & "' for property '" & $asProperties[$iCount] & "' exists" & @CRLF
    ElseIf @error = 1 Then
    $sOutput &= "Object '" & $asObjects[$iCount] & "' for property '" & $asProperties[$iCount] & "' does not exist" & @CRLF
    Else
    $sOutput &= "Object '" & $asObjects[$iCount] & "' for property '" & $asProperties[$iCount] & "' is not unique: >=" & @error & " records found" & @CRLF
    EndIf
    Next
    MsgBox(64, "Active Directory Functions", $sOutput)

    [/autoit]

    Ich würde gerne überprüfen, ob ein User in der Domain existiert, und als nächstes ob das bekannte Kennwort auch gesetzt ist.

    Es ist eine Multidomainumgebung - und Installationen fallen immer wieder auf die Nase, weil der User nicht angelegt wurde, oder sein Kennwort nicht gesetzt ist.. Da ist jetzt mein Plan, vor der Installation ein paar Eckdaten zu prüfen, und gegebenenfalls auszusteigen.

    Wo müsste ich da ansetzen?

  • Um in einer Multi-Domain-Umgebung zu prüfen,musst Du den Global Catalog ansprechen. Im Wiki steht, wie das funktioniert.

  • Mit Multidomain meinte ich in mehreren Domains unabhängigen Domains, sorry für das Missverständniss.

    Wie kann ich denn ein mir bekanntes Kennwort verifzieren?

  • Wieso kennst Du das Passwort eines Benutzers?
    Multidomain klingt nach einem großen Unternehmen und da gilt doch sicher eine Security-Policy, dass das Passwort nur dem Benutzer bekannt sein sollte.

  • Ich hätte da auch mal eine Frage und zwar zu _AD_IsAccountExpired.

    Obwohl der PC nicht mehr in der AD ist sagt er mir immer "has not expired".
    Was muss ich denn beachten?


    [autoit]

    ; 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)

    [/autoit][autoit][/autoit][autoit]

    ; *****************************************************************************
    ; Example 2
    ; Checks if the current computer account is expired.
    ; *****************************************************************************

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    If _AD_IsAccountExpired($pc) Then
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has expired")
    Else
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has not expired")
    EndIf

    [/autoit][autoit][/autoit][autoit]

    ; Close Connection to the Active Directory
    _AD_Close()

    [/autoit]


    Gruß
    Norman

  • Surfy.

    Hier hast Du ...

    [autoit]


    Global $Err = ObjEvent('AutoIt.Error', '_ComErr')

    [/autoit][autoit][/autoit][autoit]

    ; Anmeldename ohne Zusätze
    _UserAuthenticate('Test', '123')

    [/autoit][autoit][/autoit][autoit]

    ; NETBIOS-Anmeldename
    _UserAuthenticate('domain\Test', '123')

    [/autoit][autoit][/autoit][autoit]

    ; User Principal Name
    _UserAuthenticate('Test@domain.local', '123')

    [/autoit][autoit][/autoit][autoit]

    Func _UserAuthenticate($Username, $Password) ; As Boolean
    Local Const $ADS_SECURE_AUTHENTICATION = 1
    Local $dso = ObjGet('LDAP:') ; As IADsOpenDSObject

    [/autoit][autoit][/autoit][autoit]

    ConsoleWrite(StringFormat('Authentifiziere %s ...\n', $Username))
    Local $dse = $dso.OpenDSObject('LDAP://dc=domain,dc=local', _ ; As IADs
    $Username, $Password, $ADS_SECURE_AUTHENTICATION)

    [/autoit][autoit][/autoit][autoit]

    If @error Then
    ConsoleWrite('Anmeldung fehlgeschlagen!' & @CRLF)
    Return False
    Else
    ConsoleWrite('Anmeldung erfolgreich!' & @CRLF)
    Return True
    EndIf
    EndFunc ;==>_UserAuthenticate

    [/autoit][autoit][/autoit][autoit]

    Func _ComErr()
    ConsoleWrite(StringFormat('\tFehlernummer: %s\n', Hex($Err.Number, 8)))
    ConsoleWrite(StringFormat('\tBeschreibung: %s\n', $Err.Windescription))
    SetError(1)
    EndFunc ;==>_ComErr

    [/autoit]
  • Prüf doch sicherheitgshalber, dass @error nach _AD_IsAccountExpired nicht gesetzt ist.
    Wie gibst Du den computer account an? Als SamAccountName? Dann brauchst Du unbedingt ein abschliessendes "$" Zeichen also z.B: @ComputerName & "$"

  • "mit ohne"? Dann versuch's mal "mit mit" $ am Ende. Und gib an, was der Wert von @error nach dem Funktionsaufruf ist.
    SamAccountName: Guckst Du hier.
    Meine Funktionen akzeptieren aber auch den FQDN (full qualified domain name)

  • Zitat

    "mit ohne"?

    :D

    Und gib an, was der Wert von @error nach dem Funktionsaufruf ist.

    Du meinst das so?

    [autoit]

    ; 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)

    [/autoit][autoit][/autoit][autoit]

    ; *****************************************************************************
    ; Example 2
    ; Checks if the current computer account is expired.
    ; *****************************************************************************

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    If _AD_IsAccountExpired($pc) Then
    if @error then
    msgbox(0,"", @error)
    endif
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has expired")
    Else
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has not expired")
    EndIf

    [/autoit][autoit][/autoit][autoit]

    ; Close Connection to the Active Directory
    _AD_Close()

    [/autoit]
  • 1 bedeutet: $sAD_Object could not be found
    Hast Du das "$" Zeichen an das Ende des PC-Namens gehängt?

  • ich habe es mit $ probiert und ohne .... immer gleiches Ergebniss.

    [autoit]

    Global $pc = "pcname$"
    ; 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)

    [/autoit][autoit][/autoit][autoit]

    ; *****************************************************************************
    ; Example 2
    ; Checks if the current computer account is expired.
    ; *****************************************************************************

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    Global $result = _AD_IsAccountExpired($pc)
    If @error Then MsgBox(0,"", @error)
    If $result Then
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has expired")
    Else
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has not expired")
    EndIf

    [/autoit][autoit][/autoit][autoit]

    ; Close Connection to the Active Directory
    _AD_Close()

    [/autoit]


    Wenn ich aber einen PC überprüfe der in der Domäne ist greift @error von der Function _AD_Is_AccountExpired nicht.
    Wenn er nicht in der Domäne ist greift @error.

    Nur in beiden Fällen greift die MsgBox

    [autoit]

    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "' has not expired")

    [/autoit]

    Gruß
    Norman

  • Ich habe es jetzt wie folgt gemacht kann das so stimmen?

    [autoit]

    Global $pc = "pcname"
    ; 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)

    [/autoit][autoit][/autoit][autoit]

    ; *****************************************************************************
    ; Example 2
    ; Checks if the current computer account is expired.
    ; *****************************************************************************

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    Global $result = _AD_IsAccountExpired($pc&"$")
    If @error Then
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "$" & "' has expired")
    Else
    MsgBox(64, "Active Directory Functions", "Computer account '" & $pc & "$" & "' has not expired")
    EndIf

    [/autoit][autoit][/autoit][autoit]

    ; Close Connection to the Active Directory
    _AD_Close()

    [/autoit]
  • Dann lass für den PC mal das Beispielskript _AD_GetObjectProperties.au3 laufen (gib den Namen mit "$" an).
    Was zeigt das Attribut "accountexpires"?

  • Bei dem PC der nicht mehr in der Domäne ist zeigt bei Example 1 folgendes.
    [1]|accountExpires|0000/00/00 00:00:00

    Example 3 überspringt er.

    Bei dem PC der in der Domäne ist zeigt er in Example 1 das gleiche wie oben nur hier überspringt er Example 3 nicht, zeigt da aber auch
    [1]|accountExpires|0000/00/00 00:00:00