get-ActiveDirectoryDomainName.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<#
.SYNOPSIS This function utilizes a distinguished domain to calculate an active directory domain name. .DESCRIPTION This function converts a distinguished name into active directory domain name. .PARAMETER DN The DN of the object to pass to normalize. .OUTPUTS The FQDN of the active directory domain. .EXAMPLE Get-activeDirectoryDomainName -dn $DN .CREDITS Credit to the following website - code adapted from this location. http://lanlith.blogspot.com/2014/06/powershell-get-domain-from.html #> Function get-activeDirectoryDomainName { [cmdletbinding()] Param ( [Parameter(Mandatory = $true)] [string]$DN ) #Output all parameters bound or unbound and their associated values. write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore) #Declare function variables. [array]$functionSplitDomainName=@() [string]$functionCombinedDomainName="" #Start function processing. Out-LogFile -string "********************************************************************************" Out-LogFile -string "BEGIN GET-ActiveDirectoryDomainName" Out-LogFile -string "********************************************************************************" #Log the parameters and variables for the function. out-logfile -string ("DN to convert: "+$DN) Out-LogFile -string "Converting the distringuished name." $functionSplitDomainName = $dn -Split "," | ? {$_ -like "DC=*"} foreach ($component in $functionSplitDomainName) { out-logfile -string $component } $functionCombinedDomainName = $functionSplitDomainName -join "." -replace ("DC=", "") out-logfile -string ("The FQDN of the object based on DN: "+$functionCombinedDomainName) Out-LogFile -string "END GET-ActiveDirectoryDomainName" Out-LogFile -string "********************************************************************************" #This function is designed to open local and remote powershell sessions. #If the session requires import - for example exchange - return the session for later work. #If not no return is required. return $functionCombinedDomainName } |