I've been working on a script that validates if a Mac's binding to AD is still active or not. This is an issue that seems to be caused when the computer is off the corporate network at the time of kerberos key cycling, typically set to 14 days (see dsconfigad -show; setting it to 0 will stop this cycling, which I am currently testing to see if it stops bindings from breaking).
This has been working fairly well, except for that we are getting some false failure reports when using the id command.
Has anyone had to do this and found a more consistent way to see if a device binding is working or broken using the id, dscl or other commands?
#!/bin/sh
adserver="directory.contoso.com"
testuser="_MacComputer"
pass="AD Binding OK"
fail="AD Binding Failed"
offline="Not in range of DC"
notbound="The Mac is not bound to contoso.com"
# Check if we can ping the AD Domain Controller, if ping was successful check if we can query a UPN
# If the ping was successful check if we can we query a UPN?
if ping -c 3 $adserver &> /dev/null; then
    # Check the domain returned with dsconfigad
    domain=$( dsconfigad -show | awk '/Active Directory Domain/{print $NF}' )
    # If the domain is correct
    if [[ "$domain" == "contoso.com" ]]; then
        # Check for the id of a user
        if id -u $testuser &> /dev/null; then
        # If the check was successful...
            echo "<result>$pass</result>"
        else
            # If the check failed
            echo "<result>$fail</result>"
        fi
    else
        # If the domain returned did not match our expectations
        echo "<result>$notbound</result>"
    fi
else
    # We can't see the DCs, so no way to properly check
    echo "<result>$offline</result>"
fiOriginal source: https://www.jamf.com/jamf-nation/discussions/7039/how-to-check-if-a-computer-is-actually-bound-to-the-ad
