Link to home
Start Free TrialLog in
Avatar of llarava
llaravaFlag for Afghanistan

asked on

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
Avatar of rlandquist
rlandquist
Flag of United States of America image

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

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
   )
Avatar of llarava

ASKER

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.
what are you putting in the text file?  Can you provide a sample?
Avatar of llarava

ASKER

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

Avatar of llarava

ASKER

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?
Avatar of llarava

ASKER

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

 
Avatar of llarava

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of rlandquist
rlandquist
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
Did you not bother to try my batch file then.... ??
Avatar of llarava

ASKER

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.
 
thank you for your reply and explanation.