Get AD container information

noah_swanson
New Contributor

Is there a way to get the Mac's AD container information? I am anticipating that this will come up when auditing so it would be great to have this be a part of the inventory system.

I currently have our McAfee virus defs version and date do the same (using the reportVirusScanDefs.sh from the Resource Kit) and was hoping to do this for AD location as well.

Noah Swanson
Imaging Specialist
Enterprise Desktop Services
Phone: 309-765-3153

10 REPLIES 10

rmanly
Contributor III

dscl localhost -read /Active Directory/All Domains/Computers/computer_name
| grep -A 1 distinguished

Ryan M. Manly
Level 4 Mac Tech
Glenbrook High Schools
1835 Landwehr Rd.
Glenview, IL 60026
(847) 486-4948

? ACSP ? ACMT

noah_swanson
New Contributor

First, Works great! Here's what I get back:

wdxedsmac07:~ edsmacadmin$ dscl localhost -read /Active Directory/All Domains/Computers/wdxedsmac07 | grep -A 1 distinguished
dsAttrTypeNative:distinguishedName: CN=WDXEDSMAC07,OU=Standard,OU=Corporate,OU=Workstations,DC=domain,DC=domain,DC=com
dsAttrTypeNative:dn: CN=WDXEDSMAC07,OU=Standard,OU=Corporate,OU=Workstations,DC=domain,DC=domain,DC=com

Anyway I can get only one of those lines to display?

Also, can this be automated instead of typing the computer name before "| grep..." ?

Thanks!

rmanly
Contributor III

I was actually just playing with dscl and realized you can query for the
relevant key without calling grep. :)

But to answer your question grep is used to display the line that matches
your query. The "-A 1" part tells it to print the line that matches and 1
more line After that. So, yes you could change what what comes after grep to
search for something different. The following is a better way though.

dscl localhost -read /Active Directory/All Domains/Computers/wdxedsmac07 dn

and it will return only:

dsAttrTypeNative:dn:
CN=WDXEDSMAC07,OU=Standard,OU=Corporate,OU=Workstations,DC=domain,DC=com

As far as automation is concerned I whipped this up. The "test' in `cat
test` is just a list I made of 5 computer names.

#!/bin/bash

for i in `cat test`; do

dscl localhost -read /Active Directory/All Domains/Computers/$i dn

done

exit 0

I called it like this ./test.sh >> done.txt and then had a list of all the
output. This included the key name but that can be edited out later via
script or even Excel-fu.

Ryan M. Manly
Level 4 Mac Tech
Glenbrook High Schools
1835 Landwehr Rd.
Glenview, IL 60026
(847) 486-4948

? ACSP ? ACMT

tlarkin
Honored Contributor

If you read the /Computers list in dscl it should generate a list of all
computer names and loop it that way

rmanly
Contributor III

I thought of that but I noticed that all the computer records don't actually
show up there for me…

Specifically 3 of my test machines that I included in the list of 5
mentioned above. They still query successfully but were not in the list. Do
you know what this might mean?

Ryan M. Manly
Level 4 Mac Tech
Glenbrook High Schools
1835 Landwehr Rd.
Glenview, IL 60026
(847) 486-4948

noah_swanson
New Contributor

Excellent.

As far as automation I would like this to be reported to JSS inventory on a weekly basis, so when someone logs in to the web-interface and creates a list, the computer’s container information will be displayed.

Thanks for the help so far!
--Noah

tlarkin
Honored Contributor

By default the dscl command looks at the localhost for information. Try
using the LDAPv3 path in your dscl command

dscl /LDAPv3/Myserver.com/Computers

noah_swanson
New Contributor

What about this:

dscl localhost –read /Active Directory/All Domains/Computers/networksetup –getcomputername dn

However then I get this above my information:

cp: /Library/Preferences/SystemConfiguration/preferences.plist.old: Permission denied

Anyway to dismiss the cp line?

lance_ogletree
Contributor

This is where the new extension attributes coming in 7.2 will help you.

dingleyd
New Contributor

Is there a way to list computers located in a selected OU?

dscl "/Active Directory/All Domains" -list Computers

Gives me a list of computers located in the root ou, which is our company's list of domain controllers. Each site gets its own Domain Controller to add its computers.

In interactive mode of dscl I can manually jump from dc to dc, but I still can't list the computers.

My goal is to be able to list each computer in each dc and sub ou.

Any ideas?
Thanks
~David