Printed 2014/10/24 09:35AM

What's my (fully qualified domain) name?

By Robert Davis, 2009/04/11

What's my name?

Fully qualified domain name that is. One of the little gotchas I encountered when rewriting my database mirroring automation scripts in powershell was that there is no simple way to get the SQL Server's fully qualified domain name. I didn't want to resort to opening a connection to the SQL Server to run a T-SQL query. There had to be a better way through powershell.

I started looking at ways to ping or query DNS via powershell. I went down the ping highway first and wasn't convinced that it was the right way to go. I changed my thinking to querying DNS and was surprised at just how simple querying DNS is with powershell.

With a single call to the GetHostEntry function of, I was able to get the fully qualified domain. The syntax is as follows:

          []::GetHostEntry("Machine Name").HostName

For the database mirroring script, I need to call the function 2 or 3 times depending on if I am configuring a witness. I assign a variable to the function for each server passing in the appropriate server's name. I then use the variables for creating the TCP connection strings for assigning the mirroring partners and witness.

Can it stand alone?

I wrote a little scriptlet that can be reused to make the process easy. The script accepts a machine name as a parameter, but if you don't give it a machine name, it will just use the name of the local machine.

          Param ($Machine)

          if (!$Machine) {$Machine = read-host "Enter machine name"}

          if (!$Machine) {$Machine = get-content env:Computername}

          $FQDN = []::GetHostEntry($Machine).HostName

          return $FQDN

Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.