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,


more ▼

asked May 05, 2012 at 01:28 PM in Default

avatar image

114 6 9 11

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 07, 2012 at 12:33 PM

avatar image

12.1k 30 36 42

Hi, thanks for the tip.!

It won't work if the server in the LOGONSERVER variable went down after login...!

May 07, 2012 at 11:24 PM JD
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 05, 2012 at 01:28 PM

Seen: 1080 times

Last Updated: May 07, 2012 at 11:25 PM

Copyright 2018 Redgate Software. Privacy Policy