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 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.
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
Nice trick. Would love to see more about how you do this in mirroring, maybe another blog or article.
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. :)
The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them. Backup a database Restore a database Scan a server to find a free port Query DNS to get the FQDN of a server
To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.
Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine. August issue of TechNet Magazine's SQL Q&A column