Tuesday, December 2, 2014

PowerShell Script to change the CNAME Target host name on the DNS server

#PowerShell Code to change the CNAME Target host name in the DNS, this was designed to meet, #failover of the servers and need to be triggered on the DNS server to switch to Stand-by server




param(
[string] $ServerName=$(throw "DNS Server parameter is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $ContainerName=$(throw "Container Name or Domain Name example.com required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $ownername=$(throw "Provide the Cname is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $newprimaryName=$(throw "target Host Name to be changed is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com")
)

#Write-Host $serverName, $ContainerName, $ownerName, $newprimaryName
$rec=Get-WMIObject -Computer $ServerName `
    -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$ContainerName' AND OwnerName='$ownerName'"
$newprimaryName = $newprimaryName + "."
#Write-Host $newprimaryName


if ($newprimaryName.equals($rec.PrimaryName))
{
Write-Host "You are passing the same target hostname to be changed"
exit(2)
}
else
{
$res = $rec.modify($rec.TTL,$newprimaryName)
}


$ret=Get-WMIObject -Computer $ServerName `
    -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$ContainerName' AND OwnerName='$ownerName'"


if ($newprimaryName.equals($ret.PrimaryName))
{
Write-Host "Successful change:"
Write-Host $ret.PrimaryName
exit(0)
}


$ret=$null
$rec=$null

No comments:

Post a Comment