Last active
October 10, 2015 10:04
-
-
Save Cirzen/854c1f704fe48df4156b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .Synopsis | |
| Gets NTBiosName for a given domain | |
| .DESCRIPTION | |
| Uses .Net framework and COM objects only. No reliance on AD Cmdlets | |
| Adapted from 'http://rlmueller.net/NameTranslateFAQ.htm#Powershell%20example' by Richard L. Mueller | |
| Adapted by David Johnson https://gist.github.com/Cirzen | |
| .PARAMETER Domain | |
| Text string to use as the search criterion | |
| .PARAMETER Credential | |
| PSCredential object (such as output by Get-Credential) used to connect to the domain. | |
| .EXAMPLE | |
| PS> Get-NetBiosName -Domain 'Contoso.com' | |
| .EXAMPLE | |
| PS> $cred = Get-Credential | |
| PS> "Contoso.com","Fabrikam.net"|Get-NetBiosName -Credential $cred | |
| #> | |
| function Get-NetBiosName | |
| { | |
| [CmdletBinding()] | |
| Param | |
| ( | |
| # Domain string to search, e.g. 'Mydomain.com' | |
| [Parameter(Mandatory=$true, | |
| ValueFromPipeline=$true, | |
| Position=0)] | |
| [string[]] | |
| $Domain, | |
| # Credential to connect to domain | |
| [Parameter(Mandatory=$false, | |
| ValueFromPipeline=$false, | |
| Position=1)] | |
| [System.Management.Automation.PSCredential] | |
| $Credential | |
| ) | |
| Begin { | |
| $DCType = [System.DirectoryServices.ActiveDirectory.DirectoryContextType]::Domain | |
| } | |
| Process { | |
| If ($Credential) { | |
| $DirectoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext( | |
| $DCType, | |
| $Domain, | |
| $Credential.UserName, | |
| $Credential.GetNetworkCredential().Password) | |
| } | |
| Else { | |
| $DirectoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext( | |
| $DCType, | |
| $Domain) | |
| } | |
| # Retrieve Distinguished Name of specified domain | |
| Try { | |
| $ADDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DirectoryContext) | |
| } | |
| Catch [System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException] { | |
| $Msg = "No valid domain found from search string '$Domain'" | |
| Throw New-Object System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException($Msg) | |
| } | |
| Catch [System.Security.Authentication.AuthenticationException] { | |
| $Msg = 'Logon failure - run again passing a valid PSCredential object (e.g. from Get-Credential) to the -Credential parameter' | |
| Throw New-Object System.Security.Authentication.AuthenticationException($Msg) | |
| } | |
| $Root = $ADDomain.GetDirectoryEntry() | |
| $Base = ($Root.DistinguishedName) | |
| # Use the NameTranslate object | |
| $objTrans = New-Object -ComObject 'NameTranslate' | |
| $objNT = $objTrans.GetType() | |
| # Define constants | |
| New-Variable -Option Constant -Name ADS_NAME_INITTYPE_GC -Value 3 | |
| New-Variable -Option Constant -Name ADS_NAME_TYPE_1779 -Value 1 | |
| New-Variable -Option Constant -Name ADS_NAME_TYPE_NT4 -Value 3 | |
| If ($Credential) { | |
| # Invoke the InitEx method to initialise NameTranslate by locating the Global Catalog | |
| $objNT.InvokeMember( | |
| 'InitEx', | |
| 'InvokeMethod', | |
| $Null, | |
| $objTrans, | |
| ($ADS_NAME_INITTYPE_GC, | |
| $Null, | |
| $Credential.GetNetworkCredential().UserName, | |
| $Credential.GetNetworkCredential().Domain, | |
| $Credential.GetNetworkCredential().Password | |
| ) | |
| ) | |
| # Use the Set method to specify the Distinguished Name of the specified domain | |
| $objNT.InvokeMember('Set', 'InvokeMethod', $Null, $objTrans, ($ADS_NAME_TYPE_1779, ($Base.ToString() ))) | |
| # Use the Get method to retrieve the NetBios name of the specified domain. | |
| # The value retrieved includes a trailing backslash | |
| $strDomain = $objNt.InvokeMember('Get', 'InvokeMethod', $Null, $objTrans, $ADS_NAME_TYPE_NT4) | |
| } | |
| Else { | |
| # Run the same as above but use Init method rather than InitEx which doesn't take credentials | |
| $objNT.InvokeMember('Init', 'InvokeMethod', $Null, $objTrans, ($ADS_NAME_INITTYPE_GC, $Null)) | |
| $objNT.InvokeMember('Set', 'InvokeMethod', $Null, $objTrans, ($ADS_NAME_TYPE_1779, ($Base.ToString()))) | |
| $strDomain = $objNt.InvokeMember('Get', 'InvokeMethod', $Null, $objTrans, $ADS_NAME_TYPE_NT4) | |
| } | |
| New-Object -TypeName PsObject -Property @{ | |
| NetBiosName = ($strDomain.Substring(0,$strDomain.Length - 1)) | |
| BaseDN = $Base[0] | |
| } | |
| } | |
| End { | |
| # Dispose of Com Object | |
| [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($objTrans) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment