Jump to content

Getting a list of computer objects in an OU to text file


Recommended Posts

Hello all,

I've been scavenging the forums here for a solution to my problem with no luck. Essentially what I'm trying to do is query the active directory for computers in the same OU as the computer I'm on (secondary want because I could specify this myself), and write the list of computer names to a text file.

The project is for school sites (I work in the district office) and to have computer lab aides push files to their students desktops (I will do this part with xcopy, which I'm comfortable with)

So far I've seen to use adfunctions.au3, I've downloaded v3.11. My program has an #include <adfunctions.au3>. The first line of the below I think is right (I got the info out of ldp.exe which seems a good browser), the second is where I'm utterly lost, is there a guide on how to shape these? I apologize, I'm not very script savvy but I'm trying to learn.

CODE
$ou= "OU=Staff,OU=Auxillary,OU=All Sites,DC=losal,DC=local"; My OU

_ADGetObjectsInOU($User, $ou, "(&(objectCategory=user))", 2, "distinguishedName,cn,department")

Edited by mikethetechguy
Link to comment
Share on other sites

Hi mikethetechguy,

Please try:

#include <adfunctions.au3>

Global $asComputers
Global $sOU = "OU=Staff,OU=Auxillary,OU=All Sites,DC=losal,DC=local"

_ADGetObjectsInOU($asComputers, $sOu, "(objectClass=computer)", 1, "name,operatingSystem,operatingSystemServicePack")
_ArrayDisplay($asComputers)oÝ÷ Ù: zܨºº0åÂ¥vëjëh×6$FQDN = _ADSamAccountNameToFQDN(@username)

This returns the full qualified domain name of the currently logged on user.

HTH

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

That does sound great, it compiles without problem and runs. Forgive my noobishness, but am I supposed to see it running? I tried

CODE
FileWrite(@ScriptDir & "\debug.txt", $asComputers[0])

but it gives me a "subscript used with a non-array variable. From loading multiple lines into a single variable (which I think is what's happening) and the _ArrayDisplay command I'd have assumed it was an array, but I must be missing something here.

Also just so I can help myself with further questions, how do you know how to shape the _ADGetObjectsInOU inside bits? Thank you so very much for taking the time to direct me on this.

Link to comment
Share on other sites

am I supposed to see it running?

No. Before calling _ADGetObjectsInOU $asComputers is a simple variable. After the call it's an Array.

Some examples on how to create LDAP query strings can be found at:http://www.petri.co.il/ldap_search_samples...nd_exchange.htm

The complete RFC can be found at http://www.faqs.org/rfcs/rfc2254.html

Happy reading :)

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 a wonderful resource thank you water! I will read through it to help figure out the ldap query bits.

What I meant was I'm not sure the purpose of _ArrayDisplay($asComputers), if it's supposed to show me something or not, like pop up a window, or write a file, or something, just not sure, I figured my filewrite would show me that info but it reads that $asComputers isn't an array which leads me to believe it isn't a string at that point due to a botched query.

So in the adfunctions.au3, I see the following

_ADGetObjectsInOU(ByRef $ObjectArray, $ou, $filter = "(name=*)", $searchscope = 2, $datatoretrieve = "sAMAccountName", $sortby = "sAMAccountName")

We fed it a variable to be an array, an OU structure, I'm thinking by the link you sent @petri.co that it ought to be "(objectCategory=computer)" rather than "(objectClass=computer)". It seems the filter is the only thing that petri.co thing is used for, so let me know if Im mistaken on that. So next in the adfunctions _Adgetobjectsinou bit is the searchscope which you've selected 1 for, which I'm assuming is because we only want one thing? For data to retreive you've put name,OS, servicepack, which I assume was a recycle from another project? I'd assume I'd only need something like "computername" but how do I know what is available to use for this part? It kind of looks like the "systeminfo" command info but isn't. hmm, I'm stumped as to where you got the variables you're using for this.

Link to comment
Share on other sites

What I meant was I'm not sure the purpose of _ArrayDisplay($asComputers)

It should simply display the content of the Array $asComputers. If you get an error use
#include <Array.au3>
in your script.

We fed it a variable to be an array

Don't bother about that. The size and dimensions depend on the number of computers returned and the number of fields you define in $datatoretrieve. The function "converts" the ariable to an array of the size/dimension needed.

It seems the filter is the only thing that petri.co thing is used for

That's right. The link gives a few examples for LDAP query strings because to create a correct query string from the RFC seems to be a bit of a challenge.

... searchscope which you've selected 1 for

From the UDF: $searchscope : optional, default 2. 0 = base, 1 = one-level, 2 = sub-tree

1 searches only in the given OU. 2 searches the given OU and all subtrees. http://www.microsoft.com/technet/scriptcen...sg0405.mspx#EYC gives a better explanation than I can (my English is a bit rusty). There you'll find the difference between objectClass and objectCategory as well.

but how do I know what is available to use for this part?

I would recommend an AD browser. ADExplorer from Sysinternals is free and doesn't need to be installed. ADSearchAdmin has more features but needs to be installed - the tools is in english but the site is in german.

I use ADSearchAdmin because it easily lets you search for Users, Computers, shows the Structure of the AD, lets you run predefined LDAP queries, supports WMI and more ...

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

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