{"id":144,"date":"2019-01-17T03:38:07","date_gmt":"2019-01-17T08:38:07","guid":{"rendered":"http:\/\/itblog.ldlnet.net\/?p=144"},"modified":"2019-01-17T04:28:09","modified_gmt":"2019-01-17T09:28:09","slug":"removing-a-dns-a-record-through-powershell","status":"publish","type":"post","link":"https:\/\/itblog.ldlnet.net\/index.php\/2019\/01\/17\/removing-a-dns-a-record-through-powershell\/","title":{"rendered":"Removing a DNS Record through Powershell"},"content":{"rendered":"\n<p>In most environments, an admin usually just jumps on the server that they need to work from and does their work from there. An example of this would be an admin working on an IIS Web server and needing to remove a DNS A record from DNS without having to logon to the DNS server itself so that they can quickly make their changes in IIS.<\/p>\n\n\n\n<p>A quick way to do this would be to run the following ps1 script in PowerShell in order to be able to remove the record quickly:<\/p>\n\n\n<pre class=\"lang:PowerShell\" title=\"RemoveDNSARecord.ps1\">$NodeToDelete = Read-Host \"Please Input the Name of the A Record you want to delete. NO FQDN\"\n$DNSServer = Read-Host \"Please Input your DNS Server FQDN\"\n$ZoneName = Read-Host \"Please Input the DNS Zone the A Record is residing in\"\n$NodeDNS = $null\n$NodeDNS = Get-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -Node $NodeToDelete -RRType A -ErrorAction SilentlyContinue\nif($NodeDNS -eq $null){\n    Write-Host \"The DNS A Record You Were Looking For Was Not Found\" -ForeGroundColor Red\n} else {\n    Remove-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -InputObject $NodeDNS -Force\n    Write-Host \"Your DNS A Record $NodeToDelete Has Been Removed\" -ForeGroundColor Green\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSARecord.png\" alt=\"Sample Output from the script.\" class=\"wp-image-146\" width=\"622\" height=\"193\" srcset=\"https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSARecord.png 570w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSARecord-300x93.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><figcaption>Sample Output from the Script removing DNS A Record: <em>test.ldlnet.local<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now this works for a single DNS A Record. If there are multiple IPs for the same DNS record, for example, test.ldlnet.local points to both 192.168.1.23 <em>and<\/em> 192.168.1.24, then you probably need to run the following script listed here to keep the script from failing with an error. I have also expanded the entries to help the input be more specific:<\/p>\n\n\n<pre class=\"lang:PowerShell\" title=\"RemoveDNSRecord.ps1\">Write-Host \"This script will remove a DNS Record based on the information provided\" -ForegroundColor Yellow\nWrite-Host \"There can be multiple Host Records (IPs) for a given Host Name. Only one Record going with the matching IP address that is input will be deleted\" -ForegroundColor Red\nWrite-Host \"For Example: In the ldlnet.org zone there are two records test.ldlnet.org > 192.168.1.23 and test.ldlnet.org > 192.168.1.24. This action removes only one of the entries of test.ldlnet.org, matching the IP address that was input.\" -ForegroundColor Red\n$NodeToDelete = Read-Host \"Please Input the Name of the DNS Record you want to delete. (NO FQDN)\"\n$DNSServer = Read-Host \"Please Input your DNS Server FQDN\"\n$ZoneName = Read-Host \"Please Input the DNS Zone the DNS Record is residing in\"\n$RecordType = Read-Host \"Please Input the Type of DNS Record It Is (A, CNAME, TXT, etc...)\"\n$IP = Read-Host \"Please Input the IP Address of the Associcated DNS Record\"\n$NodeDNS = $null\n$NodeDNS = Get-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -Node $NodeToDelete -RRType $RecordType -ErrorAction SilentlyContinue\nif($NodeDNS -eq $null){\n    Write-Host \"The DNS A Record You Were Looking For Was Not Found\" -ForeGroundColor Red\n} else {\n    Remove-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -RecordData $IP -Name $NodeToDelete -RRType $RecordType -Force -ErrorAction Stop\n    Write-Host \"Your DNS A Record $NodeToDelete Has Been Removed\" -ForeGroundColor Green\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSRecord.png\" alt=\"\" class=\"wp-image-150\" width=\"670\" height=\"232\" srcset=\"https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSRecord.png 884w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSRecord-300x104.png 300w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/01\/RemoveDNSRecord-768x267.png 768w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><figcaption>Output from RemoveDNSRecord.ps1 for removing DNS A Record <em>test.ldlnet.local<\/em> with IP of 192.168.1.24<\/figcaption><\/figure><\/div>\n\n\n\n<p>I have found some other good scripts that I will post to the blog to help manage DNS records through PowerShell. This should get things started for now. Happy Troubleshooting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In most environments, an admin usually just jumps on the server that they need to work from and does their work from<\/p>\n<p class=\"link-more\"><a class=\"myButt \" href=\"https:\/\/itblog.ldlnet.net\/index.php\/2019\/01\/17\/removing-a-dns-a-record-through-powershell\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,2,3,16],"tags":[58,15,57,8,13,20],"class_list":["post-144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-active-directory","category-general","category-powershell","category-windows","tag-a-record","tag-cmdlet","tag-dns","tag-powershell","tag-script","tag-windows","odd"],"_links":{"self":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":4,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions\/151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media\/147"}],"wp:attachment":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}