Jump to content
Sign in to follow this  
amir

Listing active directory users

Recommended Posts

amir

When i try to execute the code down below, i get :

C:\ADT.au3 (22) : ==> The requested action with this object has failed.:

Local $objRecordSet = $objCommand.Execute

Local $objRecordSet = $objCommand.Execute^ ERROR

Does anyone have any idea why?

(i got this code from an old post on the forum)

Local $objCommand = ObjCreate("ADODB.Command")
Local $objConnection = ObjCreate("ADODB.Connection")

$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

;Local $strBase = "<GC://dc=YOURDOMAINNAME,dc=COM>"
Local $strBase = ObjGet("LDAP//RootDSE") 
Local $strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))"
Local $strAttributes = "cn,sAMAccountName,displayName,sn,distinguishedName"
Local $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree"


$objCommand.CommandText = $strQuery
$objCommand.Properties ("Page Size") = 100
$objCommand.Properties ("Timeout") = 30
$objCommand.Properties ("Cache Results") = False
$ADS_SCOPE_SUBTREE = 2
$objCommand.Properties ("searchscope") = $ADS_SCOPE_SUBTREE

Local $objRecordSet = $objCommand.Execute

ConsoleWrite("Users"&@CR&"---------"&@CR)

While Not $objRecordSet.EOF
         $strName = $objRecordSet.Fields ("sAMAccountName").Value
         $strCN = $objRecordSet.Fields ("cn").value
         $strdisplayName = $objRecordSet.Fields ("displayName").value
         $strSN = $objRecordSet.Fields ("SN").value
         $strdistinguishedName = $objRecordSet.Fields ("distinguishedName").value
   ;FileWriteLine("Users.txt",$objRecordSet.Fields ("sAMAccountName").Value)
        ConsoleWrite($objRecordSet.Fields ("sAMAccountName").Value&@cr)
    $objRecordSet.MoveNext
WEnd

$objConnection.Close
$objConnection = ""
$objCommand = ""
$objRecordSet = ""

Share this post


Link to post
Share on other sites
amir

OK, this finally worked for me:

The key line for me was:

Local $strBase = "<LDAP://dc=YourDOMAINname,dc=DOMAINsuffix>"

Local $objCommand = ObjCreate("ADODB.Command")
Local $objConnection = ObjCreate("ADODB.Connection")

$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

Local $strBase = "<LDAP://dc=YourDOMAINname,dc=DOMAINsuffix>"

Local $strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))"
Local $strAttributes = "cn,sAMAccountName,displayName,sn,distinguishedName"
Local $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree"


$objCommand.CommandText = $strQuery
$objCommand.Properties ("Page Size") = 100
$objCommand.Properties ("Timeout") = 30
$objCommand.Properties ("Cache Results") = False
$ADS_SCOPE_SUBTREE = 2
$objCommand.Properties ("searchscope") = $ADS_SCOPE_SUBTREE

$objRecordSet = $objCommand.Execute

ConsoleWrite("Users"&@CR&"---------"&@CR)

While Not $objRecordSet.EOF
         $strName = $objRecordSet.Fields ("sAMAccountName").Value
         $strCN = $objRecordSet.Fields ("cn").value
         $strdisplayName = $objRecordSet.Fields ("displayName").value
         $strSN = $objRecordSet.Fields ("SN").value
         $strdistinguishedName = $objRecordSet.Fields ("distinguishedName").value
       ;FileWriteLine("Users.txt",$strName)
        ConsoleWrite($strdisplayName&@cr)
        ConsoleWrite($strName&@cr)
        ConsoleWrite(@cr)
        $objRecordSet.MoveNext
WEnd

$objConnection.Close
$objConnection = ""
$objCommand = ""
$objRecordSet = ""

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.