DNS Server - PowerShell - Domain Controller Removal from Name Servers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    DNS Server - PowerShell - Domain Controller Removal from Name Servers

    All,
    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.

    Code:
    		#FQDN of the domain controller that has been decommissioned or is offline
    		$oldDMCName = "DMC1.domain.org"
    		
    		#Get the PDC Emulator
    		$PDCe = Get-ADDomainController -Discover -Service PrimaryDC
    		
    		#Get all DNS zones on the PDCe
    		$DNSZones = Get-DnsServerZone -ComputerName $PDCe
    		
    		#Iterate DNS zones and remove the stale domain controller record where applicable
    		ForEach($zone in $DNSZones)
    		  {
    		    $zone = $zone.zoneName
    		    $getZoneInfo2 = (Get-DnsServerResourceRecord -ZoneName $zone -Name "@" -RRType NS -ComputerName $PDCe).recorddata.nameserver
    		    If($getZoneInfo -like "*$oldDMCName*")
    		      {
    		        Try
    		          {
    		            Remove-DNSServerResourceRecord -ZoneName $zone –Name “@” –RRType NS –RecordData $oldDMCName -ComputerName $PDCe -Force
    		          }
    		        Catch
    		          {
    		            Write-Output "Error removing $oldDMCName from $zone"
    		          }
    		      }
    		    Else
    		      {
    		        Write-Output "$oldDMCName does not exist in zone $zone"
    		      }
    		  }
  • 88myr1xbet
    New Member
    • Aug 2023
    • 1

    #2
    great man, that's amazing question to ask

    Comment

    • anwabrand
      New Member
      • Sep 2024
      • 2

      #3
      To remove a domain controller from the DNS server’s name servers list using PowerShell, you can use the Remove-DnsServerResour ceRecord cmdlet. Here's the process: Steps:
      1. Identify the DNS record of the domain controller you want to remove.
      2. Remove the DNS record using PowerShell.
      PowerShell Command:
      1. Open PowerShell as Administrator.
      2. Use the following commands, replacing the placeholders with your specific details:
      powershell
      Copy code
      # Variables $ZoneName = "yourdomain .com " # Replace with your domain name $RecordName = "dc01" # Replace with the name of the domain controller to be removed $Server = "DNSserverN ame" # Replace with your DNS server name # Remove the DNS A record of the domain controller Remove-DnsServerResour ceRecord -ZoneName $ZoneName -Name $RecordName -ComputerName $Server -Force Explanation:
      • $ZoneName: The DNS zone where the domain controller's record resides.
      • $RecordName: The hostname of the domain controller.
      • $Server: The name of the DNS server you are targeting.

      This script forcefully removes the DNS A record for the domain controller from the specified DNS server.

      Make sure to replace the placeholders with your actual zone name, domain controller name, and DNS server name. Let me know if you need any further assistance!

      Comment

      Working...