Jump to content

Search Active Directory


Recommended Posts

Here is what I want to do, but am not sure of how exactly to do it...

; Search the Domain for all OUs containing Computers

??

; Do something with each computer name

What I have right now (and want to do differently):

Dim $SearchOus[7]

; Set some Constants
Global $objConnection = ObjCreate("ADODB.Connection"); Create COM object to AD
$objConnection.ConnectionString = "Provider=ADsDSOObject"
$objConnection.Open("Active Directory Provider"); Open connection to AD

$SearchOus[0]="OU=Execs,OU=workstations,OU=MAIN"
$SearchOus[1]="OU=Grunts,OU=workstations,OU=MAIN"
$SearchOus[2]="OU=Old,OU=MAIN"
$SearchOus[3]="OU=Techs,OU=workstations,OU=MAIN"
$SearchOus[4]="OU=ShortBus,OU=workstations,OU=MAIN"
$SearchOus[5]="OU=MajorProject,OU=workstations,OU=MAIN"
$SearchOus[6]="OU=workstations,OU=MAIN"

$LogFile = FileOpen($ResultsFile, $ForWriting)
If @error = -1 Then
    MsgBox(0,"Error","ERROR: Unable to initialize requested log file, " & $ResultsFile & ".")
    FileClose($ResultsFile)
    Exit
EndIf;Err.Number <> 0
ProgressOn("Working...", "Beginning script", "Please wait...",2,-2,18)

For $strOU In $SearchOus
    $objOU = ObjGet("LDAP://" & $strOU & ",DC=YourCompany,DC=local")
;$objOU.Filter = "Computer"
    For $Computer in $objOU
        $strPCName = ($Computer.cn)
        Call("DoSomething", $strPCName)
    Next
Next
Edited by Graywalker
Link to comment
Share on other sites

Search for adfunctions.au3. This udf has all the functions you need.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Search for adfunctions.au3. This udf has all the functions you need.

Did that before posting, searched through it - it didn't have what I needed.

If you saw a _ADListComputers function, point it out please. _ADListDomainControllers was about as close as it got.

Link to comment
Share on other sites

Figured it out! The code below grabs every "computer" in the AD and calls the function on each one.

; YOUR DIMS above here

Global $objConnection = ObjCreate("ADODB.Connection"); Create COM object to AD

$objConnection.ConnectionString = "Provider=ADsDSOObject"

$objConnection.Open("Active Directory Provider"); Open connection to AD

$objAD_Recordset = ObjCreate("ADODB.Recordset")

$objAD_Recordset.Open ("SELECT * FROM 'LDAP://DC=YOURDOMAIN,DC=YOURS' WHERE objectClass='computer'", $objConnection,3,3,0x0001)


Do
    $strADPC = $objAD_Recordset.Fields(0).value

    $objPC = ObjGet($strADPC)

    $strPCName = ($objPC.cn)

        Call("DoSomething", $strPCName)

    $objAD_Recordset.MoveNext()

Until $objAD_Recordset.EOF()
    
Exit

; YOUR FUNCTIONS HERE

You can also get more info about the computer with $objPC..operatingSystem or .operatingSystemServicePack, .operatingSystemVersion, .whenChanged, .pwdLastSet, etc.

Edited by Graywalker
Link to comment
Share on other sites

Hi Graywalker,

to get what you need you would have to use the function _ADGetObjectsInOU of the adfunctions UDF and create a LDAP query to select the required information.

Some of my postings have further information how to create LDAP queries.

If you have further questions just drop me a note.

Thomas

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi Graywalker,

to get what you need you would have to use the function _ADGetObjectsInOU of the adfunctions UDF and create a LDAP query to select the required information.

Some of my postings have further information how to create LDAP queries.

If you have further questions just drop me a note.

Thomas

Can you give an example of how that would let me get a list of every single Computer object in the entire domain?

Link to comment
Share on other sites

*Sigh* Actually... the last "Solution" works fine if you have less than 1000 computers in your Active Directory... we, however, have around 4000.

So...

; Prepare Active Directory and Query
Global $objConnection = ObjCreate("ADODB.Connection"); Create COM object to AD
$objConnection.ConnectionString = "Provider=ADsDSOObject"
$objConnection.Open("Active Directory Provider"); Open connection to AD
$objAD_Command = ObjCreate("ADODB.Command")
$objAD_Command.ActiveConnection = $objConnection
$objAD_Command.CommandText = "SELECT * FROM 'LDAP://DC=YOURDOMAIN,DC=YOURS' WHERE objectClass='computer'"
$objAD_Command.Properties("Page Size") = 1000
$objAD_Command.Properties("Searchscope") = 2
$objAD_Recordset = $objAD_Command.Execute
$RecordCount = $objAD_Recordset.recordcount

Do
    $strDN = $objAD_Recordset.Fields(0).value
    $objPC = ObjGet($strDN)
    $strPCName = ($objPC.cn)
    $pcc = $pcc+1; Counts the number of computers you've completed for the progress window.
        $i = 1
        ProgressSet($i, $pcc & " of " & $RecordCount, "Working on " & $strPCName)
        Call("DoSomething", $strPCName, $objPC, $pcc)
    $objAD_Recordset.MoveNext()
Until $objAD_Recordset.EOF()

I should probably note that this carries the OBJECT of the PC over - with the entire fully distinguished name and all. So you can, in your "DoSomething" Function still do things like $OS = $objPC.operatingSystem and such. You would need to turn the progress on with whatever you wanted, but the $pcc keeps a PC count so you can tell where the script is in the task - and relatively how long it has to finish.

Edited by Graywalker
Link to comment
Share on other sites

That's quite easy

$sOU = "DC=microsoft,DC=com"
_ADGetObjectsInOU($asUser, $sOU, "(objectCategory=computer)", 2, "cn, operatingsystem")
Returns an array ($asUser) of all computers in your domain (defined by $sOU). For every computer the AD fields cn and operatingsystem are returned.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

That's quite easy

$sOU = "DC=microsoft,DC=com"
_ADGetObjectsInOU($asUser, $sOU, "(objectCategory=computer)", 2, "cn, operatingsystem")
Returns an array ($asUser) of all computers in your domain (defined by $sOU). For every computer the AD fields cn and operatingsystem are returned.
Ah... I guess I was stuck with it looking for USERS and thinking it required an actual OU.... doah!

That would do a list...

Of course, I'd probably need to add a variable where you got "cn, operatingsystem" ;

something like

$returns = "cn, distinguishedName, operatingSystem, operatingSystemServicePack, operatingSystemVersion, whenCreated, whenChanged, etc"

I did use the _ADRecursiveGetMemberOf function, that worked out very good. (what I needed the distinguished name for)

For my current purposes, I think what I came up with works better, but will keep this in mind for later projects - Thank you!

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