• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

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
0
llarava
Asked:
llarava
  • 6
  • 3
  • 3
1 Solution
 
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
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now