VBscript to delete Domain Zone and Host A-Record

Posted on 2007-08-01
Last Modified: 2010-05-18
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:


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

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 "","","",,,""
objA.CreateInstanceFromPropertyData "","","",,,""
objA.CreateInstanceFromPropertyData "","","",,,""

Question by:ksbunger1
    LVL 10

    Assisted Solution

    by:Walter Padrón

    Author Comment

    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.
    LVL 10

    Accepted Solution


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

    LVL 65

    Assisted Solution

    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     )


        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"
            Wscript.Echo "    Deletes the zone"
            Wscript.Echo "2.  DnsZones.vbs Delete /s MyMachine2"
            Wscript.Echo "    Deletes the zone 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(""".""")
        Set objDNS = objService.Get("MicrosoftDNS_Zone.ContainerName=""" & strPropertyArray(0) & """,DnsServerName=""" & objServer.Name & """,Name=""" & strPropertyArray(0) & """")
        If blnErrorOccurred("Error Deleting zone.") then Exit Sub
        Call WriteLine("Zone deleted from server " & & ".", objOutputFile)
        If IsObject(objOutputFile) Then
            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



    Author Comment

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

    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
       For Each objRR In objRRs
    End If

    MsgBox "Host Deleted!"


    Dim oLocator, oService
    Dim objDNS

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

    Set objServer = oService.Get(""".""")
    Set objDNS = oService.Get("MicrosoftDNS_Zone.ContainerName="""",DnsServerName=""" & objServer.Name & """,Name=""""")

    MsgBox "Zone Deleted!"
    LVL 10

    Expert Comment

    by:Walter Padrón
    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!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now