Jump to content

Listing active directory users


Recommended Posts

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 = ""
Link to comment
Share on other sites

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 = ""
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...