C# 目录搜索器

C# 目录搜索器,c#,powershell,C#,Powershell,我最初编写了一个脚本来在PowerShell中生成Outlook签名,现在我想将其转换为一个C#程序,具有模板构建和自定义等额外功能 初始脚本的美妙之处在于,您只需向它传递一个用户名,其余的都由它完成。提取信息,创建目录结构,输出HTML等 我遇到的问题是从C#中的广告中提取信息。我一直试图通过DirectoryServices名称空间实现这一点。我想我已经基本掌握了它应该如何工作,应该做什么,但是我不断地犯错误,好像我错过了一些重要的事情,比如类型转换,或者如何初始化应用程序中可用的数据 这是

我最初编写了一个脚本来在PowerShell中生成Outlook签名,现在我想将其转换为一个C#程序,具有模板构建和自定义等额外功能

初始脚本的美妙之处在于,您只需向它传递一个用户名,其余的都由它完成。提取信息,创建目录结构,输出HTML等

我遇到的问题是从C#中的广告中提取信息。我一直试图通过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");

您必须尝试将对象打印到屏幕上,这就是问题的原因。尝试打印对象属性。如果您使用错误更新问题,则将非常有用。没有错误很难想象您的问题是什么。请注意,您可能希望添加某种验证,以确保用户输入的数据不会简单地导致程序因奇怪的错误而崩溃。同意,我肯定会使用try-and-catch块,但现在,我正在努力确保我知道如何制作项目的每一部分,然后将其串在一起,然后我将担心如何确保用户不会执行不希望的操作。正如猜测一样,您是否可以尝试将其构建为一个32位进程并运行它?如果这消除了错误,我们可以考虑为什么会发生这种情况。它解决了最初的问题,但似乎还有另一个问题。如果您只想让它使用域中的任何DC并指定要搜索的确切OU,那么它的格式是什么?请尝试不使用server:port,但保留协议:“LDAP://OU=users,DC=domain,DC=com”遇到另一个错误,然后尝试使用我使用的主FQDN而不是服务器名。然后,我认为只有在初始化搜索结果变量时才会出现问题。