Jump to content
Sign in to follow this  

Read an Active Directory Attribute

Recommended Posts


Hi guys!

I would like to read the Attribute "Location" of a printer.

This is how my script looks:

; _ADGetObjectAttribute
    ; Retrieves the specified (single-value) attribute for the given SamAccountName
    ; Returns 0 if the object does not exist, the attribute does not exist for that
    ; object or if the value is multi-string.
    ; Otherwise returns the result

    Dim $objConnection
    Dim $strHostServer
    Dim $strDNSDomain
    $object = "PNMarketingC22PCL"
    $attribute = "Location"

    MsgBox(0,"klsda","Result: " & _ADGetObjectAttribute($object,$attribute))

    Func _ADGetObjectAttribute($object, $attribute)
        If _ADObjectExists($object) = 0 Then Return 0

        $strHostServer = "server27"
        $strDNSDomain = "CN=PVSVPS001"
        $strQuery = "<LDAP://" & $strHostServer & "/" & $strDNSDomain & ">;(sAMAccountName=" & $object & ");ADsPath;subtree"
        $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the object
        $ldap_entry = $objRecordSet.fields (0).value
        $oObject = ObjGet($ldap_entry)  ; Retrieve the COM Object for the object
        $result = $oObject.Get ($attribute)
        $oObject = 0
        If $result = "" Then
            Return ""
            Return $result
    EndFunc   ;==>_ADGetObjectAttribute

    Func _ADObjectExists($object)
        $strQuery = "<LDAP://" & $strHostServer & "/" & $strDNSDomain & ">;(sAMAccountName=" & $object & ");ADsPath;subtree"
        $objRecordSet = $ObjConnection.Execute ($strQuery)  ; Retrieve the FQDN for the group, if it exists
        If $objRecordSet.RecordCount = 1 Then
            $objRecordSet = 0
            Return 1
            $objRecordSet = 0
            Return 0
    EndFunc   ;==>_ADObjectExists

When I start, I get this error:

H:\AutoIt\Printer\AD_read_prop.au3 (39) : ==> Variable must be of type "Object".: 
$objRecordSet = $ObjConnection.Execute ($strQuery) 
$objRecordSet = $ObjConnection^ ERROR



Share this post

Link to post
Share on other sites

Have you tried using ObjCreate instead of ObjGet?

Edited by DarkMatter

[sub]Quantum mechanics: The dreams stuff is made of[/sub]

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.