Reputation: 1672
I initially wrote a script to generate Outlook signatures in PowerShell, and now I want to turn it into a C# program with extra features like template building and customization, etc.
The beauty of the initial script is that you simply pass a user name to it, and it does the rest. Pulls the info, creates a directory structure, spits out HTML etc.
What I'm having trouble with is pulling the info from AD in C#. I've been trying to get to this with the DirectoryServices namespace. I think I have a basic grasp of how it's supposed to work, and what it's supposed to do, but I keep getting errors as if I'm missing something important like a type conversion, or how to initialize the data to be usable inside the app.
Here's my code, and I don't know what's wrong with it:
Console.Write("What User do you want properties for?:");
string usr = Console.ReadLine();
DirectoryEntry dir = new DirectoryEntry("OU=users,DC=domain,DC=com");
DirectorySearcher find = new DirectorySearcher(dir, "(&(objectClass=User)(enabled=true)(SAMAccountName=" + usr + "))");
find.PropertiesToLoad.Add("SAMAccountName");
find.PropertiesToLoad.Add("GivenName");
find.PropertiesToLoad.Add("Surname");
find.PropertiesToLoad.Add("StreetAddress");
find.PropertiesToLoad.Add("City");
find.PropertiesToLoad.Add("State");
find.PropertiesToLoad.Add("PostalCode");
find.PropertiesToLoad.Add("OfficePhone");
find.PropertiesToLoad.Add("HomePhone");
find.PropertiesToLoad.Add("Fax");
find.PropertiesToLoad.Add("EmailAddress");
find.PropertiesToLoad.Add("Pager");
Console.WriteLine(find.Filter);
SearchResult res = find.FindOne();
The error is around where I try to print to screen to ensure I have the right info. As shown here:
Console.Write(res);
Console.ReadLine();
Edit: Additional information.
The issue appears to be when executing
SearchResult res = find.findOne();
Also the actual error is
Unhandled Exception: System.Runtime.InteropServices.COMException: Unspecified Error
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectorySerivices.DirectoryEntry.get_AdsObject()
at System.DirecotryServices.DirecotrySearcher.FindAll(Boolean findMoreThanOne)
at System.DirecoryServices.DirectorySearcher.FindOne()
at ConsoleApplication1.Program.Main(String[] args)
Also, compiler output. I was initially under the impression that this was because the machine I'm coding on isn't part of the domain I'm checking against, but I've also been running the executable on a machine that is.
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=Unspecified error
Source=System.DirectoryServices
StackTrace:
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at ConsoleApplication1.Program.Main(String[] args) in c:\users\administrator\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 36
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Upvotes: 2
Views: 1872
Reputation: 308
Include the protocol, server, and port in the path:
DirectoryEntry dir = new DirectoryEntry("LDAP://servername:port/OU=users,DC=domain,DC=com");
Upvotes: 2