PowerShell: Script to make batch DNS changes
Typically when you host websites that require 99.999% uptime you create a basic webfarm that sits behind an F5 BigIP ... not every company has a budget to do high availablity or DR correctly. So I wasn't shocked when I was tasked to write a script to modify DNS CNAME records target host FQDN with a new FQDN.
Welcome to my situation :) If we lose an IIS server we would have to move all the sites that box was hosting to another and then update the CNAME's target fully qualified domain name field.
Found a bunch of great stuff online on how to modify a single record ... that's great for updating maybe a handful, not if it's over 100+ records and I'm not about to do that manually via MMC console! So I need to get a little creative and here's what I came up with.
$CNAMES = import-csv "Path to CSV file"
$Query = "Select * from MicrosoftDNS_CNAMEType"
Foreach($CNAME in $CNAMES)
{
$CNAME
$Record = Get-WmiObject -Namespace "root\microsoftdns" -Query $Query -ComputerName dnsserver | Where-Object{$_.Ownername -match $CNAME.Aliases}
$Record.RecordData = "FQDN of new IIS server"
$Record.put()
}
The script can be modified to update any kind of DNS record, so it's not locked into just updated CNAME's.
I pretty much have a bunch of CSV files that contain a single colmn called Aliases, these CSV files hold all the CNAME aliases for all our sites. As you can see in the first line of the script I pretty much delcare $CNAMES to import in all the data from the CSV file to be used later in the script. I then declare what kind of DNS record I'm looking for as $Query.
In order to apply the change to each row in the CSV file I needed to place the PowerShell command that will be making the change in a Foreach statement, hence the Foreach($CNAME in $CNAMES). The next few lines are pretty much the meat and potatoes of the script and is pretty self explanitory and accurate with what you might find searching the web.
Now with the $Record.RecordData = "FQDN of new IIS server" can easily be changed to allow the person running the script to enter what FQDN to change to manually. By simply changing that line to $Record.RecordData = Read-Host "Enter the new Target Host FQDN", the Read-Host cmdlet asks for user input on the console allowing the user to type whatever he/she wants ... what is typed in the console the script will use to modify the CNAME with.
Of course this script is available for you to use in anyway shape of form ... but I do not take responsibility if something goes wrong. As any administrator will tell you, test first and test again. Any script examples I post is 'use at your own risk'. Look at what the script is doing rather than copy, paste, and execute. Don't know what a certain parameter or commandlet does - Google search :) Thanks.
Matt Seidel