如何以编程方式在 Active Directory 中搜索打印机

时间:2023-01-01
本文介绍了如何以编程方式在 Active Directory 中搜索打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

尝试使用 C# 在 Active Directory 中查找打印机/共享.

Attempting to find printers / shares in Active Directory using C#.

这是我的示例代码,适用于用户,但是我无法找到使用相同概念的打印机.(我是 Active Directory 的新手).

This is my sample code that works for users however I cannot seen to be able to find a printer using the same concept. (I am new to Active Directory).

    DirectoryEntry entry = new DirectoryEntry();
    entry.Path = "LDAP://xxx.xxx.xx.xx/CN=Printers;DC=domainName, DC=com";
    entry.Username = @"domainName.comAdministrator";
    entry.Password = "admin";

    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(objectCategory=printQueue)";
    SearchResult result = search.FindOne();

    if (result != null)
    {
        ResultPropertyCollection fields = result.Properties;

        foreach (String ldapField in fields.PropertyNames)
        {

            foreach (Object myCollection in fields[ldapField])
                Console.WriteLine(String.Format("{0,-20} : {1}",
                              ldapField, myCollection.ToString()));
        }
    }

如有任何帮助,我们将不胜感激.

Any assistance would be greatly appreciated.

推荐答案

与用户 (CN=Users) 相反,Active Directory 中没有 CN=Printers 容器安装后.

In contrast to users (CN=Users) there is no CN=Printers container in Active Directory after installation.

打印机发布在相关计算机容器中的 Active Directory 中.有什么作用相关的计算机容器是什么意思?好吧,打开 Active Directory 用户和计算机 MMC 管理单元,然后请按照以下步骤操作:

Printers are published in Active Directory in the releated computer container. What does releated computer container mean? Well, open Active Directory Users and Computers MMC snap-in and follow this procedure:

  1. 在视图菜单中选择高级功能.
  2. 在视图菜单中选择用户、联系人、组和计算机作为容器.
  3. 导航到计算机对象(现在显示为容器)您的打印机属于.
  4. 单击计算机容器的加号.在那里你会看到打印机对象.

因此,您会看到打印机在 Active Directory 中的相关计算机容器(打印机所属)中发布,而不是在一个常见容器中,例如 CN=Printers.

So, you see printers are published in Active Directory in the releated computer container (the printer belongs to) and not in one common container such as CN=Printers.

因此,要在 Active Directory 中搜索打印机对象,您必须指定不同的 LDAP 路径.例如,您可以指定 Active Directory 的根目录作为搜索根:

So, to search for a printer object in Active Directory, you have to specify a different LDAP path. For example you could specify the root of your Active Directory as the search root:

using (DirectoryEntry entry = new DirectoryEntry())
{
  entry.Path = "LDAP://xxx.xxx.xxx.xxx/DC=domainName,DC=com";
  entry.Username = @"domainName.comAdministrator";
  entry.Password = "SecurePassword";

  using (DirectorySearcher search = new DirectorySearcher(entry))
  {
    search.Filter = "(objectCategory=printQueue)";
    SearchResult result = search.FindOne();

    if (result != null)
    {
      ResultPropertyCollection fields = result.Properties;

      foreach (String ldapField in fields.PropertyNames)
      {
        foreach (Object myCollection in fields[ldapField])
          Console.WriteLine(String.Format("{0,-20} : {1}",
                          ldapField, myCollection.ToString()));
      }
    }
  }
}

当然,您也可以将打印机所在计算机的 LDAP 路径指定为搜索根目录共享于.例如,如果您的打印机在名为 server10 的计算机上共享,并且此计算机位于 CN=Computers 容器中,则指定此 LDAP 路径:

Of course, you could also specify as search root the LDAP path to the computer where your printer is shared on. For example if your printer is shared on a computer called server10 and this computer is located in the CN=Computers container, then specify this LDAP path:

LDAP://xxx.xxx.xxx.xxx/CN=server10,CN=Computers,DC=domainName,DC=com

如果您在域控制器上共享打印机,则 LDAP 路径略有不同(因为默认情况下域控制器计算机对象位于 OU=Domain Controllers 组织单位中):

If you share a printer on the domain controller then the LDAP path is slightly different (because by default domain controller computer objects are located in the OU=Domain Controllers organizational unit):

LDAP://xxx.xxx.xxx.xxx/CN=DomainControllerName,OU=Domain Controllers,DC=domainName,DC=com

这篇关于如何以编程方式在 Active Directory 中搜索打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:GroupPrincipal 方法 FindByIdentity 抛出奇怪的异常 下一篇:使用 c# 的 Active Directory 属性列表

相关文章

最新文章