PowerShell to help Web Site Launch – Multiple Host Headers on Multiple Web Servers
Quite often I receive requests to launch a website with multiple domain names and they will need to work with or with out www such as whatever.com and www.whatever.com. The site need to be launched on NLB cluster nodes. I got the contents from developers and copy them over to the production web servers and make necessary configurations – routine stuff.
But if the request is multiple domain names to point to the same site, the task is daunting! 30-50 domains, I have to create DNS zones one by one. Our external DNS is hosted on Windows 2000 and I am hesitate to install PowerShell on it! So for now, I create the dns zones manually (expect to upgrade the DNS server to Windows 2008 soon)!
How about Host Headers, 30 domains multiple by 2 for the www and set them up on two nodes NLB cluster, the picture is not pretty if you do them manually!
Let’s say I got the domain names from business in below format:
whatever.com
whatever1.com
whatever2.com
whatever3.com
whatever4.com
All point to whatever.com and people on the internet should hit them with or without www. Here is my solution:
Save the above domain name in DNSZones.txt
run the little script -
foreach ($zone in (gc .\DNSZones.txt))
{$www = "www." +$Zone;add-content ".\DNSZones.txt" $www}
Now the DNSZones.txt looks like this:
whatever.com
whatever1.com
whatever2.com
whatever3.com
whatever4.com
www.whatever.com
www.whatever1.com
www.whatever2.com
www.whatever3.com
www.whatever4.com
Next run the below script -
New-Item "C:\Users\yl\Documents\PS\Hostheader.txt" -Type file
foreach ($zone in (gc .\DNSZones.txt))
{$IPBinding = "192.168.1.23:80:" +$Zone;add-content ".\Hostheader.txt" $IPBinding}
Now the file will be look like below – 192.168.30.23 is the NLB cluster VIP
192.168.30.23:80:whatever.com
192.168.30.23:80:whatever1.com
192.168.30.23:80:whatever2.com
192.168.30.23:80:whatever3.com
192.168.30.23:80:whatever4.com
192.168.30.23:80:www.whatever.com
192.168.30.23:80:www.whatever1.com
192.168.30.23:80:www.whatever2.com
192.168.30.23:80:www.whatever3.com
192.168.30.23:80:www.whatever4.com
Why I go through all these trouble and have a file format like that? That’s the Metabase.xml serverbinding format:
I have direct metabase edit enabled on my web servers, I make a BACKUP copy of the metabase.xml file and then open the live one in notepad and search for whatever.com and underneath that, I look for ServerBindings=" “ and paste the above text file inside the quote and save the file. Open the website property and verify all the host headers are in place! Repeat the same thing for the other NLB nodes. Isn’t that cool!
Wait, I am not out of the woods yet, with that many domain names, everyone involved could easily make a mistake somewhere and we don’t want the business or customer find that out first – how you verify all the domain names resolve and point to the same site? Again, PowerShell!
Remember the DNSZones.txt? I save the below script as CheckWebs.ps1 and run it.
foreach ($domain in gc .\DomainZones.txt)
{
$ie = new-object -comobject "InternetExplorer.Application"
#$ie.visible = $true
$ie.navigate("http://" + $domain + ".com")
}
I could easily find out which domain or site is not working, so I can double check!
Only then I feel a little bit love for my job. Most the time, I hate it! :)