Script do delete a bunch of records from a DNS forward/reverse zone

Hi,

I have a list of records that needs to be deleted from a DNS zone (.txt file).  Via dnscmd I can delete any type of DNS records for a particular zone.

dnscmd dnsserver /recorddelete domainzone.local PC-4X3ZD51 A

Does someone know of any script that could read from a list that will contain the computer names (csv or txt file) and delete the records from the zone?

Thanks
llaravaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rlandquistCommented:
Give this a try and let me know if you have any issues.
strFile = "C:\scripts\input.txt"

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFile)

If objFile.Size > 0 Then
	Set objReadFile = objFSO.OpenTextFile(strFile, 1)
		While Not objReadFile.AtEndOfStream
		strcomputer = objReadFile.ReadLine
		While strcomputer <> ""
		objshell.Run "dnscmd dnsserver /recorddelete domainzone.local " & strcomputer & " A"
		Wend
	Wend
	objFile.Close
Else
	WScript.Echo "The source file does not exist."
End If

Open in new window

0
t0t0Commented:
Does this work (I'm not familoiar with DNSCMD)...

Copy and paste the code into Notepad and save it a batch file. Fire up a DOS box and run the batch file from the command line. You should run it passing the name of the text file as a command line parameter like this:

   DELRECS computers.txt

where DELRECS is the name of the batch file and 'computers.txt' is the name of your text file containing a list of computer names.


@echo off
if "%~1"=="" (
   echo.
   echo Missing filename.
   echo.
   echo SYNTAX: %~nx0 ^<filename^>
   echo.
   echo Where ^<filename^> is a text file containing a list of computer names
   echo.
   exit /b 1
)

if not exist "%~1" (
   echo.
   echo Cannot find %~1. Program aborted.
   echo.
   exit /b 2
)

for /f "tokens=* usebackq" %%a in ("%~1") do (
   dnscmd dnsserver /recorddelete domainzone.local %%a A
)

exit /b 0



NOTE: If your list of computernames are in CSV format then I can tweak the code to work with that too. Please let me know...


NOTE: The actual code that does your job is:

   for /f "tokens=* usebackq" %%a in ("%~1") do (
      dnscmd dnsserver /recorddelete domainzone.local %%a A
   )

There is no reason why you can't just use this (I've included the text filename):

   @echo off
   for /f "tokens=* usebackq" %%a in ("computers.txt") do (
      dnscmd dnsserver /recorddelete domainzone.local %%a A
   )
0
llaravaAuthor Commented:
Hi,

I appologize for the delay.

I have tried rlandquist script however the it creates a loop. It does not delete the record I have added to the .txt file and enters in some sort of loop that never ends.

Can you please advise?

Thanks.
0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

rlandquistCommented:
what are you putting in the text file?  Can you provide a sample?
0
llaravaAuthor Commented:
Hi,

For now I have only one entry like SERVER1

The scripts runs:

objshell.Run "dnscmd dnsserver /recorddelete domainzone.local " & strcomputer & " A"

I think the problem is that  the command bring a confirmation question like "Are you sure?"

dnscmd dnsserver /recorddelete domainzone.local  

The question can be eliminated with /f so the final command will look like that

dnscmd /recoddelete domain.com server1 A /f

Is the code below correct? that maybe the issue.

What do you think?


objshell.Run "dnscmd dnsserver /recorddelete domainzone.local " & strcomputer & " A" & "/f" 

Open in new window

0
llaravaAuthor Commented:
I don't think I am passing the /f correctly to the code to force the delete command and avoid getting the question that I believe is what is causing the loop.

objshell.Run "dnscmd dnsserver /recorddelete domainzone.local " & strcomputer & " A" & "/f"

Is that all right?
0
llaravaAuthor Commented:
I have modified the code in order to get an echo of the output:

strRunCommand = "dnscmd /recorddelete /domain.com " & strcomputer & " A" & " /f"  
Wscript.Echo strRunCommand

The output doesn't show that the script is taking the entry on the txt (name of the computer), however the command is build they way it should.  

This is the output:

dnscmd /recorddelete domain.com A /f

So basically I am missing the computer name

dnscmd /recorddelete domain.com computer1 A /f

 
0
llaravaAuthor Commented:
I have made changes to the code:

I am getting error

Line 12 Char 53
Expeted end of statement

Any thoughts?
strFile = "C:\scripts\input.txt"  
  
Set objShell = CreateObject("WScript.Shell")  
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile = objFSO.GetFile(strFile)  
  
If objFile.Size > 0 Then  
        Set objReadFile = objFSO.OpenTextFile(strFile, 1)  
        Do While Not objReadFile.AtEndOfStream  
                strcomputer = objReadFile.ReadLine  
                If strcomputer <> "" then 
                        objshell.Run "objshell.Run "dnscmd dnsserver /recorddelete domain.local " & strcomputer & " A" & "/f                              
                End If      
        Loop 
        objReadFile.Close  
Else  
        WScript.Echo "The computer does not exist."  
End If

Open in new window

0
rlandquistCommented:
Give this a try.  You need the space between the A and the / and you are missing the end quote mark.
objshell.Run "objshell.Run "dnscmd dnsserver /recorddelete domain.local " & strcomputer & " A /f"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t0t0Commented:
Did you not bother to try my batch file then.... ??
0
llaravaAuthor Commented:
t0t0,

Thanks for the batch the only issue is that vbs it is more understantable to me. Also I am trying to keep all the scripts in vbs. In the past our previous batchs were converted into vbs just to keep everything the same since it was easy to maintain.
 
0
t0t0Commented:
thank you for your reply and explanation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.