Find a Mac's Active Directory name?

mbuckner
Contributor

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 15

jarednichols
Honored Contributor
dsconfigad -show | grep "Computer Account"

mbuckner
Contributor

That was exactly what I needed! Thanks!

mbuckner
Contributor

That was exactly what I needed! Thanks!

shakim
New Contributor III

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"

jarednichols
Honored Contributor
#!/bin/sh

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

mm2270
Legendary Contributor III

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.

jarednichols
Honored Contributor

This will get a cleaner result:

#!/bin/sh

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

shakim
New Contributor III

Perfect!! This works like a charm. Thank you

stevenjklein
Contributor II

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

MatG
Contributor III

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
Legendary Contributor III

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

MatG
Contributor III

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
Legendary Contributor III

OK, try this then:

#!/bin/bash

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

echo "$ADDomain"

MatG
Contributor III

Awesome thanks.

joshuasee
Contributor III

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