Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

Powershell script to read txt file and remove DNS entries.

Hello,

I need to write a powershell script to read a txt files of list of IP addresses and remove the DNS entries on the DNS server (Windows 2008).  Some DNS entries has same IP address with different FQDN.  The txt file looks like the following:

10.16.20.11
10.16.20.12
10.16.20.13...and so on.  

Please advise if this is possible.

If not, we want the script to read the following and remove them from DNS:

serverA     10.16.20.11
serverB     10.16.20.12
serverC     10.16.20.13....and so on.

Thanks.
Avatar of becraig
becraig
Flag of United States of America image

ok so this is possible if you are going to have a csv file with both the host name and the ip you want to remove from dns as well as the name of the DNS server

First you have to create a csv file as below:

Server, IP
servera, 10.xx.xx.xx
serverb, 10.xx.xx.xx


Then create and run the script below from a powershell window.
For now I have output the command as a write-host so you can validate it, if the entries look good remove the write-host and the quotes that wrap the actual command and run.

import-csv c:\filename.csv | % {
$rname = $_.server + ".domain.local."
write-host "DnsCmd.exe <DNSSERVERNAME> /Recorddelete  <DOMAIN.LOCAL> $rname A $_.ip"}

Open in new window

Avatar of nav2567

ASKER

is "dnscmd.exe" a valid cmdlet?
dnscmd.exe is a built in windows command.

Should be present on any server that has the dns feature installed.
Avatar of nav2567

ASKER

Thanks.  

I copied dnscmd.exe to c:\windows\system32 and ran the following script:

import-csv "C:\dnsentries.csv" | % {
$rname = $_.server + ".domain.local."
DnsCmd.exe ourdnsserver /Recorddelete ourdomain.com $rname A $_.ip}

but it does not delete the entries I specified.  

Please advise again.

Thanks.
$rname = $_.server + ".domain.local."
DnsCmd.exe ourdnsserver /Recorddelete ourdomain.com $rname A $_.ip}


".domain.local."  needs to be replaced with your actual domain.
Avatar of nav2567

ASKER

I ran this but I do not see entries being deleted:

import-csv "c:\dnsentries.csv" | % {
$rname = $_.server + ".mydomain.com"
DnsCmd.exe mydnsserver /Recorddelete mydomain.com $rname A $_.ip}
$rname = $_.server + ".mydomain.com"

needs to have a "." at the end

$rname = $_.server + ".mydomain.com."
Avatar of nav2567

ASKER

Just tried that with same result:

import-csv "c:\dnsentries.csv" | % {
$rname = $_.server + ".mydomain.com."
DnsCmd.exe mydnsserver /Recorddelete mydomain.com $rname A $_.ip}
Can you share the actual command that the script creates (redact the sensitive info)
Avatar of nav2567

ASKER

I copy and paste the exact commands you suggest and replace the name of mydomain and mydnsserver.
I just need to verify the command syntax is correct:
e.g


DnsCmd.exe SERVER /Recorddelete  domain.local Server.domain.local. A 10.17.8.1

This is a windows native command, if your syntax is correct and your DNS server and domain info is correct an the record is a dns record that actually exists there is no way for this to fail.
So something must be off in the command that we are running or the dns server we are pointing to.
Avatar of nav2567

ASKER

I finally got it to work when I run the script from a domain controller instead of a member server.  

It is prompting me to confirm the deletion for each entry.  How do I skip that?

Thanks.
simply add /f to the end of the command

DnsCmd.exe SERVER /Recorddelete  domain.local Server.domain.local. A 10.17.8.1 /f
Avatar of nav2567

ASKER

Thanks.

So, I will use this in the script?

import-csv "c:\dnsentries.csv" | % {
 $rname = $_.server + ".mydomain.com"
 DnsCmd.exe mydnsserver /Recorddelete mydomain.com $rname A $_.ip /f}
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

Thanks.  

I need to ask another question about reading a text file of the same thing to add DNS entries.  I am going to submit another question.

Thanks for your help again!!
Glad I could help.
brilliant script!