Skip to main content
Question

Find a Mac's Active Directory name?

  • October 18, 2012
  • 15 replies
  • 81 views

Forum|alt.badge.img+7

How can I find a mac's Active Directory name from the terminal? I've got a bunch of macs with correct AD names and incorrect local names. I want to have the correct names in Casper Inventory. I know how to change the name of the computer, but I would like to automate the process. Can anyone tell me how to get the AD name?

Thanks!
Mark

15 replies

Forum|alt.badge.img+24
  • Valued Contributor
  • October 18, 2012
dsconfigad -show | grep "Computer Account"

Forum|alt.badge.img+7
  • Author
  • Contributor
  • October 18, 2012

That was exactly what I needed! Thanks!


Forum|alt.badge.img+7
  • Author
  • Contributor
  • October 18, 2012

That was exactly what I needed! Thanks!


Forum|alt.badge.img+13
  • Contributor
  • December 7, 2012

How can i set this up as an Extension Attribute? So JSS can track the hostname and the preferred domain host?

dsconfigad -show | grep "Preferred Domain controller"


Forum|alt.badge.img+24
  • Valued Contributor
  • December 7, 2012
#!/bin/sh

ADname=`dsconfigad -show | grep -i "preferred domain controller"`
echo <result>$ADname</result>

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • December 7, 2012

Look at existing Extension Attribute templates in your JSS for how they work. The general idea is to echo back the results of what your script finds (usually in a variable) within <result> and </result> tags surrounded by quotes.


Forum|alt.badge.img+24
  • Valued Contributor
  • December 7, 2012

This will get a cleaner result:

#!/bin/sh

PDC=`dsconfigad -show | grep -i "preferred domain controller" | sed -n 's/[^.]*= //p'`
echo <result>$PDC</result>

Forum|alt.badge.img+13
  • Contributor
  • December 11, 2012

Perfect!! This works like a charm. Thank you


Forum|alt.badge.img+6
  • Contributor
  • April 26, 2017

This will get a cleaner result for the first script to show the AD name:

#!/bin/sh

ADname=`dsconfigad -show | grep -i "Computer Account" | sed -n 's/[^.]*= // ; s/.$//p'`
/bin/echo "<result>$ADname</result>"

exit

Forum|alt.badge.img+10
  • Valued Contributor
  • February 14, 2018

Just trying this but it cuts off the last character of the retuned domain

so my company.net shows as mycompany.ne

Any ideas?

ADname=dsconfigad -show | grep -i "Active Directory Domain" | sed -n 's/[^.]*= // ; s/.$//p' /bin/echo "$ADname"

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • February 14, 2018

@MatG The AD domain is something that Jamf Pro already captures natively. Is there a reason you're trying to make an Extension Attribute for this? The original post and some of the responses below it were about capturing the computer account name as it would show up inside Active Directory.

And to answer your question, it's getting cut off because of the final sed command, which is stripping off the trailing character, but that was intentional in the original script since AD computer names always have a $ at the end of them.


Forum|alt.badge.img+10
  • Valued Contributor
  • February 14, 2018

I'm not intending to use as EA but as part of AppleScript to show domain user is on.

I thought it was the sed, but cant understand the syntax.


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • February 14, 2018

OK, try this then:

#!/bin/bash

ADDomain=$(dsconfigad -show | awk -F'= ' '/Active Directory Domain/{print $NF}')

echo "$ADDomain"

Forum|alt.badge.img+10
  • Valued Contributor
  • February 14, 2018

Awesome thanks.


Forum|alt.badge.img+17
  • Valued Contributor
  • February 14, 2018

Ah, everybody has their favorite recipe for the soup.

This might tread a little more lightly than awk:

ADDomain=$(dsconfigad -show | sed -n 's/Active.Directory.Domain.*.=.//p');
echo "<result>${ADDomain}</result>"

The will tread much more heavily than other solutions, but can handle pretty much any name or encoding imaginable. It might be handy with other fields containing unusual characters.

ADDomain=$(/usr/libexec/PlistBuddy -c 'Print ":General Info:Active Directory Domain" ' /dev/stdin <<< $(dsconfigad -xml -show));
echo "<result>${ADDomain}</result>"