目录搜索者
本文关键字:搜索者 | 更新日期: 2023-09-27 18:07:15
我最初写了一个脚本来在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:
在路径中包含协议、服务器和端口:
DirectoryEntry dir = new DirectoryEntry("LDAP://servername:port/OU=users,DC=domain,DC=com");