Find the NetBios Name of a domain in AD

Its been quite a bit of struggle for me to find an accurate way of finding the netbios name of a domain from AD using System.DirectoryServices.

In case you are in the same jam here how you do it.

  1. Connect to AD using the following ldap url:

    LDAP://CN=Partitions,CN=Configuration,DC=,DC=<local|com>

  2. When querying AD using the Directory Searcher object uses the following filter:

    netbiosname=*

This should give you a record from AD containing the netbios name of the domain as the CN

Explanation

AD stores the the netbios name in the Partitions naming container which is stored inside the configuration naming container.

A more detailed explanation and more samples can be found in the Active Directory Cookbook or its online version

Code sample:

// Method call string netBiosName = GetNetBiosName( LDAP://CN=Partitions,CN=Configuration,DC=<DomainName>,DC=<local|com>,  "<userName"", "<password>"); // Method call // Method Definition private string GetNetBiosName(     string ldapUrl,     string userName,     string password) {     string netbiosName = string.Empty;     DirectoryEntry dirEntry = new DirectoryEntry(ldapUrl,             userName, password);     DirectorySearcher searcher = new DirectorySearcher(dirEntry);     searcher.Filter = "netbiosname=*";     searcher.PropertiesToLoad.Add("cn");     SearchResultCollection results = searcher.FindAll();     if (results.Count > 0)     {         ResultPropertyValueCollection rpvc = results[0].Properties["CN"];         netbiosName = rpvc[0].ToString();     }     return netbiosName; }

Crossposted from tariqayad.com

This article is part of the GWB Archives. Original Author: Tariq

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.