How to dinamically query the first available Active Directory Servers
Hi everybody, I use this code to query Active Directory, with the AD server name hardcoded: `SET @PCdis=(SELECT COUNT(*) FROM OPENQUERY(ADSI,'SELECT Name,distinguishedName FROM ''LDAP://MY_AD_SERVER/OU=XX,OU=Domain Computers,DC=XXX,DC=XXX,DC=XX'' WHERE objectCategory = ''computer'' AND userAccountControl= 4130'));` Recently, my Active Directory server went off-line and I have to change the code of the query to point another AD Server in the forest, my question is: Is there any way to dinamically query the most-available AD server for querys instead of coding the server name..? Best regards, JD
If you can drop out to a command prompt or query the registry you can find a value from the system environment variable **LOGONSERVER**. This usually requires a logged on user to populate though. An alternative is to PING the domain name for an IP.