Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Create a Hash Table with AD Domain DNS Root and NetBIOS Names

Posted on September 2, 2018 by Alan

I frequently get requests to modify or lookup a list of user names in a CSV file where the username is NTDomain\SamAccountName.   Get-ADUser will let you use the NT domain as a server name, but in my experience it is slower than using the domain’s FQDN.  Before I import the list in my code, I run Get-DomainNameHashTable, which returns the hash object $domHash.  The name in $domHash is the NT/NetBIOS domain name (CONTOSO), and the value is the DNS Root/FQDN for the domain (CONTOSO.COM).

Function Get-DomainNameHashTable {
    $Forest = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest())
    $DomainList = ($Forest.Domains).Name

    #Bill Stewart
    function Invoke-Method([__ComObject] $object, [String] $method, $parameters) {
        $object.GetType().InvokeMember($method, "InvokeMethod", $NULL, $object, $parameters)
    }

    $NameTranslate = New-Object -ComObject "NameTranslate"
    #INITTYPE_GC
    Invoke-Method $NameTranslate "Init" ((3), "")
    $Script:domHash = @{}
    $domainList | 
        ForEach-Object { 
        $dn = "DC=" + $($_).replace(".", ",DC=")
        #Set is From Type 1779 Get is to Type NT4
        Invoke-Method $NameTranslate "Set" ((1), $dn)
        $NTDom = [string](Invoke-Method $NameTranslate "Get" (3)).Replace("\", "")
        $domHash.Add($NTDom.ToUpper(), $_.ToUpper())
    }
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($NameTranslate) | Out-Null
    $domHash
}

After reading the CSV file, I split the user name name on “\”, then lookup up the DNS root for Contoso with $domHash[‘Constoso’], or $domHash.item(‘Constoso’). Here is an example:

$userlist = import-csv "$env:userprofile\Desktop\userlist.csv"
Get-DomainNameHashTable
ForEach-Object ($FullName in $$userList.Name){
  $t = $FullName.split("\")
  $NTDom = $t[0]
  $SamName = $t[1]
  $Domain = $domHash[$NTDom]
  Get-ADUser -Server $domain -SamName
}

 

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy