nav2567
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.
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.
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.
Should be present on any server that has the dns feature installed.
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.
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.
DnsCmd.exe ourdnsserver /Recorddelete ourdomain.com $rname A $_.ip}
".domain.local." needs to be replaced with your actual domain.
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}
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."
needs to have a "." at the end
$rname = $_.server + ".mydomain.com."
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}
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)
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.
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.
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.
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
DnsCmd.exe SERVER /Recorddelete domain.local Server.domain.local. A 10.17.8.1 /f
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}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!
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!
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.
Open in new window