我最初编写了一个脚本来在PowerShell中生成Outlook签名,现在我想将其转换为具有模板构建和自定义等额外功能的C#程序。
初始脚本的优点在于您只需将用户名传递给它,然后完成剩下的工作。拉取信息,创建目录结构,吐出HTML等。
我遇到的问题是在C#中从AD中提取信息。我一直试图通过DirectoryServices命名空间来实现这一点。我想我已经基本掌握了它应该如何工作,以及它应该做什么,但我一直在犯错误,好像我错过了像类型转换那样重要的东西,或者如何初始化可在应用程序内使用的数据。
这是我的代码,我不知道它有什么问题:
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();
错误在我试图打印到屏幕的地方,以确保我有正确的信息。如下所示:
Console.Write(res);
Console.ReadLine();
编辑:其他信息。
执行
时似乎出现问题SearchResult res = find.findOne();
实际错误也是
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)
另外,编译器输出。我最初的印象是,这是因为我编码的机器不是我要检查的域的一部分,但我也在机器上运行可执行文件那是。
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:
答案 0 :(得分:2)
在路径中包含协议,服务器和端口:
DirectoryEntry dir = new DirectoryEntry("LDAP://servername:port/OU=users,DC=domain,DC=com");