Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

What's my (fully qualified domain) name?

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.

system.net.dns

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

          [system.net.dns]::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 = [system.net.dns]::GetHostEntry($Machine).HostName

          return $FQDN

Comments

Posted by Steve Jones on 13 April 2009

Nice trick. Would love to see more about how you do this in mirroring, maybe another blog or article.

Posted by Robert Davis on 15 April 2009

Thanks Steve. So far the powershell scripts i've posted have been excerpts from my mirroring automation script I wrote for my book. You'll be able to see the whole thing in September. :)

Leave a Comment

Please register or log in to leave a comment.