如何以编程方式搜索在Active Directory中的打印机 [英] How to programmatically search a printer in Active Directory

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

问题描述

试图使用C#找到打印机/股在Active Directory中。

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

这是我的样本code,它适用于用户,但是我不能看作是能够找到使用相同的概念,一台打印机。 (我是新来的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.com\Administrator";
    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()));
        }
    }

任何援助将大大AP preciated。

Any assistance would be greatly appreciated.

推荐答案

在此相反,以用户( CN =用户​​)没有 CN =打印机容器在Active Directory安装后。

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. 选择用户,Contancts,组和计算机作为容器在视图菜单中。
  3. 导航到计算机对象(也就是现在显示为容器) 您的打印机属于。
  4. 单击计算机容器的加号。在那里,你会看到 打印机对象。
  1. Select advanced features in the view menu.
  2. Select Users, Contancts, Groups and Computers as containers in the view menu.
  3. Navigate to the computer object (which is now displayed as container) your printer belongs to.
  4. Click on the plus sign of the computer container. There you will see the printer object.

所以,你看打印机Active Directory中发布了相关型号的电脑容器(打印机所属的),而不是一个普通的容器中,如 CN =打印机

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路径。例如,你可以指定你的活动目录的根 作为搜索根:

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.com\Administrator";
  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 =计算机容器,然后指定此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 =域控制器组织单位):

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中的打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆