Victor Vogelpoel

Excellence is in the details

PowerShell: Resolve-FQDNHostName

Leave a comment


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”
Advertisements

Author: Victor Vogelpoel

Dad, SharePoint technical specialist, PowerShell architect, photographer and just a guy whose life happens while trying planning it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s