470,822 Members | 1,256 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

DNS Server - PowerShell - Domain Controller Removal from Name Servers

300 256MB
As I head through a major refresh I realized it was way too cumbersome to manually remove decommissioned domain controllers from the 'Name Servers' tab inside of DNS forward and reverse lookup zones. I came up with the below script to automate the removal of these records. I hope this helps others in the future.

All you need to do is modify the FQDN (Fully Qualified Domain Name) on Line 2.

Expand|Select|Wrap|Line Numbers
  1.         #FQDN of the domain controller that has been decommissioned or is offline
  2.         $oldDMCName = "DMC1.domain.org"
  4.         #Get the PDC Emulator
  5.         $PDCe = Get-ADDomainController -Discover -Service PrimaryDC
  7.         #Get all DNS zones on the PDCe
  8.         $DNSZones = Get-DnsServerZone -ComputerName $PDCe
  10.         #Iterate DNS zones and remove the stale domain controller record where applicable
  11.         ForEach($zone in $DNSZones)
  12.           {
  13.             $zone = $zone.zoneName
  14.             $getZoneInfo2 = (Get-DnsServerResourceRecord -ZoneName $zone -Name "@" -RRType NS -ComputerName $PDCe).recorddata.nameserver
  15.             If($getZoneInfo -like "*$oldDMCName*")
  16.               {
  17.                 Try
  18.                   {
  19.                     Remove-DNSServerResourceRecord -ZoneName $zone –Name “@” –RRType NS –RecordData $oldDMCName -ComputerName $PDCe -Force
  20.                   }
  21.                 Catch
  22.                   {
  23.                     Write-Output "Error removing $oldDMCName from $zone"
  24.                   }
  25.               }
  26.             Else
  27.               {
  28.                 Write-Output "$oldDMCName does not exist in zone $zone"
  29.               }
  30.           }
Apr 26 '16 #1
0 8682

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Douglas G. Walker | last post: by
1 post views Thread by Michael Maes | last post: by
3 posts views Thread by Richard Chandler | last post: by
reply views Thread by Richard | last post: by
reply views Thread by Rob Roberts | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.