English 中文(简体)
• 如何从方案角度在主动名录中搜寻一台打印机
原标题:How to programmatically search a printer in Active Directory

试图利用C#在主动名录中找到打印机/份额。

这是我为用户设计的样本代码,但我不能认为能够使用同一概念找到打印机。 (我是积极名录的新人)。

    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()));
        }
    }

任何援助都将受到高度赞赏。

最佳回答

与用户(CN=Users)形成对照的是,在安装后,没有<编码>CN=Printers的集装箱。

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. 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.

因此,你看到打印机在改制的计算机集装箱(打印机属于)中以现成目录的形式出版,而不是放在一个共用集装箱内,如<编码>CN=Printers。

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()));
      }
    }
  }
}

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

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
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签