My current project involves scripting an installation and and configuration of several Microsoft products onto a myriad of servers. I found it very useful to resolve the fully qualified hostname (FQDN), for a server hostname, hence the function Resolve-FQDNHostname.
The function will use the active directory to resolve a hostname to a fully qualified hostname
function Resolve-FQDNHostName { [CmdletBinding()] param ( [Parameter(Mandatory=$false, position=0, ValueFromPipeLine=$true, ValueFromPipeLineByPropertyName=$true, HelpMessage="TODO")] [Alias('CN','__SERVER', 'Server', 'Hostname')] [String[]]$ComputerName = "localhost" ) process { foreach ($comp in $ComputerName) { if ($comp -eq ".") { $comp = "localhost" } Write-Output ([System.Net.Dns]::GetHostByName($comp).HostName) } } }
Examples of use:
- Resolve-FQDNHostname
without any arguments will return the FQDNHostname for the current server - Resolve-FQDNHostname -computerName server1
will return the FQDNHostname for “server1”: “server1.domain.local” - Resolve-FQDNHostname -computerName server1,server2,server3
will return the FQDNHostnames for each specified server - If the server cannot be resolved, the function will throw an exception: Resolve-FQDNHostName : Exception calling “GetHostByName” with “1” argument(s): “No such host is known”