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

VBscript to delete Domain Zone and Host A-Record

Im needing a VBscript that will automatically delete a Domain (Zone) and Host (A-Record) in Windows Server 2003. I currently use the following code to create a domain and host, now I just need some code to delete them if need be:

'=====================
'CREATE DOMAIN (ZONE)
'=====================

strZoneName ="sample.com"
intPrimaryZone = 0
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\root\MicrosoftDNS")

Set objItem = objWMIService.Get("MicrosoftDNS_Zone")
errResult = objItem.CreateZone(strZoneName, intPrimaryZone)

'=============================
'CREATE HOST Record (A-Record)
'==============================

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objPService = objLocator.ConnectServer(".","root\microsoftdns")
Set objA = objPService.Get("MicrosoftDNS_AType")

objA.CreateInstanceFromPropertyData "","sample.com","ww1.sample.com",,,"192.168.22.1"
objA.CreateInstanceFromPropertyData "","sample.com","ftp.sample.com",,,"192.168.22.2"
objA.CreateInstanceFromPropertyData "","sample.com","ww1.sample.com",,,"192.168.22.3"
'===========================

0
ksbunger1
Asked:
ksbunger1
  • 3
  • 2
3 Solutions
 
Walter PadrónCommented:
Try dnscmd

See http://www.petri.co.il/dnscmd_command_in_windows_2000_2003.htm and 'dnscmd /?'

Regards
0
 
ksbunger1Author Commented:
I appreciate the link. Dnscmd works great but unfortunately, This is just a small bit of code that will be going into vbscript file and will not be efficient for my project (in case of errors and other scenarios). Any other ideas?

Thanks again.
0
 
Walter PadrónCommented:
Download ftp://ftp.microsoft.com/reskit/win2000/dnsprov.zip 

The file contains several VBScript examples which allow you to accomplish most of the DNS related management tasks, check dnsrecord.vbs

Regards
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
RobSampsonCommented:
wpadron, great link you have provided there!
ksbunger1, this is the DeleteZone script from that zip file that you should be able to use:
'==================
'********************************************************************
'*
'* Sub DnsDelete()
'*
'* Purpose: Deletes a Zone.
'*
'* Input:   strServer           a machine name
'*          strOutputFile       an output file name
'*          strUserName         the current user's name
'*          strPassword         the current user's password
'*
'* Output:  Results are either printed on screen or saved in strOutputFile.
'*
'********************************************************************

Sub DnsDelete(strServer        , _
              strUserName      , _
              strPassword      , _
              strOutputFile    , _
              strPropertyArray , _
              blnContextHelp     )


    ON ERROR RESUME NEXT

    Dim objFileSystem, objOutputFile, objService, objDNS, objServer
   
    If blnContextHelp then
        Wscript.Echo ""
        Wscript.Echo "Delets a Zone."
        Wscript.Echo ""
        Wscript.Echo "SYNTAX:"
        Wscript.Echo "  DnsZones Delete [zonename]"
        Wscript.Echo "                  [/S <server>] [/U <username>] [/W <password>]"
        Wscript.Echo "                  [/O <outputfile>]"
        Wscript.Echo ""
        Wscript.Echo "PARAMETER SPECIFIERS:"
        Wscript.Echo "   server        A machine name."
        Wscript.Echo "   username      The current user's name."
        Wscript.Echo "   password      Password of the current user."
        Wscript.Echo "   outputfile    The output file name."
        Wscript.Echo ""
        Wscript.Echo "EXAMPLE"
        Wscript.Echo ""
        Wscript.Echo "1.  DnsZones.vbs Delete foo.com"
        Wscript.Echo "    Deletes the zone foo.com."
        Wscript.Echo "2.  DnsZones.vbs Delete foo.com /s MyMachine2"
        Wscript.Echo "    Deletes the zone foo.com from server MyMachine2."
        Wscript.Echo ""
        Exit Sub
    End If

    'Open a text file for output if the file is requested
    If Not IsEmpty(strOutputFile) Then
        If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
            Call Wscript.Echo ("Could not open an output file.")
            Exit Sub
        End If
    End If

    'Establish a connection with the server.
    If blnConnect("root\microsoftdns" , _
                   strUserName , _
                   strPassword , _
                   strServer   , _
                   objService  ) Then
        Call Wscript.Echo("")
        Call Wscript.Echo("Please check the server name, " _
                        & "credentials and WBEM Core.")
        Exit Sub
    End If

    set objServer = objService.Get("MicrosoftDNS_Server.name="".""")
    Set objDNS = objService.Get("MicrosoftDNS_Zone.ContainerName=""" & strPropertyArray(0) & """,DnsServerName=""" & objServer.Name & """,Name=""" & strPropertyArray(0) & """")
    objDns.Delete_
    If blnErrorOccurred("Error Deleting zone.") then Exit Sub
    Call WriteLine("Zone deleted from server " & objserver.name & ".", objOutputFile)
    If IsObject(objOutputFile) Then
        objOutputFile.Close
        Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
    End If

End Sub
'=================

Apparently you run that with a command like this:
DnsZones.vbs Delete foo.com

Regards,

Rob.
0
 
ksbunger1Author Commented:
Well dnsprov would not work correctly for me, but it got me close to my destination so I will award you points for that :) Here is the code that did the job in case anyone else may need it in the future.

'==============================
' DELETE HOST Record (A-Record)
'==============================

strComputer = "."
strHostName = "ww1.sample.com"

Set objDNS = GetObject("winmgmts:\\" & strComputer & "\root\MicrosoftDNS")
Set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")

Set objRRs = objDNS.ExecQuery(" select * " & _
                          " from MicrosoftDNS_ResourceRecord " & _
                          " where OwnerName = """ & strHostName & """" & _
                          " Or RecordData = """ & strHostName & """")
If objRRs.Count < 1 Then
   WScript.Echo "No matches found for " & strHostName
Else
   For Each objRR In objRRs
      objRR.Delete_
   Next
End If

MsgBox "Host Deleted!"


'======================
' DELETE DOMAIN (ZONE)
'======================

Dim oLocator, oService
Dim objDNS

Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oService = oLocator.ConnectServer("", "root\MicrosoftDNS")

Set objServer = oService.Get("MicrosoftDNS_Server.name="".""")
Set objDNS = oService.Get("MicrosoftDNS_Zone.ContainerName=""sample.com"",DnsServerName=""" & objServer.Name & """,Name=""sample.com""")
objDns.Delete_

MsgBox "Zone Deleted!"
0
 
Walter PadrónCommented:
I'm glad to help! This is an old link i used to install providers for MOM, really i didn't run the script or try to deep into the code, i'm not a good VB programmer like you ;)

Good luck!
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.

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