Just thought I would post about this quickly as its an error message that I ran into recently that took me quite a while to figure out. So to save other .NET developers spending ages trying to work out why it was happening like I did, I’ll explain how you can get rid of this rather annoying problem.
Symptoms:
You try to use the DirectorySearcher to find some objects in AD but get the error “The directory service is unavailable” (error number 2147016689).
Cause and Solution
This is actually be caused by the structure of your search filter, and nothing to do with the directory service being unavailable. Whilst some LDAP query utilities will let you use a filter like this to specify an “AND” operation (ie only objects that match all parts of the filter will be returned):
(objectClass=computer)(operatingSystem=Windows*)
It seems that the .NET DirectorySearcher does not like this and you need to explicitly add the ampersand character in there, like so:
(&(objectClass=computer)(operatingSystem=Windows*))
and that simple action should stop you getting this error 🙂
Thanks! You just saved me from barking up the wrong tree!
In my case, the username for the account that the context is created with was incorrect.
Thanks for saving me some time. You rock!
In my case the error occurred because the DC was being rebooted just when my script was halfway through exporting all users to a CSV file.