x

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
more ▼

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

JD gravatar image

JD
104 6 7 8

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

1 answer: sort newest

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

Blackhawk-17 gravatar image

Blackhawk-17
11.9k 28 31 36

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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x106
x26
x8

asked: May 05, 2012 at 01:28 PM

Seen: 854 times

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