Access the Microsoft DNS-Server using WMI RRS feed

  • Question

  • User306280371 posted
    Hi folks, 2 years ago, we started running our own DNS servers for our Websites. We are using the Microsoft DNS Server included in Windows 2000 Server. Setting up a new Website is a lot of work, so we wrote programs for everything. Now, we are in the ASP.NET thing, too, and changed all our scripts to ASP.NET. Accessing the DNS-Server using ASP.NET was the most complicated thing. First of all you need the DNS WMI Provider for Windows 2000 installed, it is part of the WMI SDK for Windows 2000 Server. The Installation is very simple: unpack the files und register the dnsprov.dll with regsvr32.exe. After that you can access your DNS-Server using WMI. Here is an example creating a new Zone (Domain) on a DNS-Server, setting the SOA-, the NS-records, creating an MX entry and the A-record for the host www. For more information on the DNS WMI Provider have a look at Microsofts MSDN, search for WMI and DNS. <%@ Page Language="VB" CompilerOptions="/R:System.Management.dll" Debug="true" %> <%@ import Namespace="System.Management" %> <script runat="server"> dim DNS as ManagementScope dim DNSSVR as ManageMentObject dim dnsname as string '# '# create a new Zone "domain" on the server "dnsserver" '# Sub new_Zone(dnsserver as string,domain as string) dim Zone as ManagementObject dim MC as ManagementClass dim Rec as ManagementObject dim MI as ManagementBaseObject dim domain as string=lcase(request.querystring("domain")) dim Q as ManagementObjectCollection, query as string dim qe as ManagementObjectCollection.ManagementObjectEnumerator dim A() as object, s as string, i as integer ' connect to WMI DNS=new ManagementScope("\\" & DNSServer & "\root\microsoftdns") DNSSVR=new ManagementObject(DNS,new ManagementPath("MicrosoftDNS_Server.Name=""."""),NOTHING) DNSSVR.Get() dnsname=DNSSVR.item("Name") ' Create the Zone MC=new ManagementClass(DNS,new ManagementPath("MicrosoftDNS_Zone"),Nothing) MI=MC.GetMethodParameters("CreateZone") MI("ZoneName")=domain MI("ZoneType")=1 MI("AdminEmailName")="admin.yourdomain.com" MI("IpAddr")=A MC.InvokeMethod("CreateZone", MI,Nothing) Q=QueryDNS("Select * From MicrosoftDNS_ZONE where ContainerName=""" & domain & _ """ and DNSServerName=""" & dnsname & """") Zone=first(Q) ' change the SOA Q=QueryDNS("Select * From MicrosoftDNS_SOAType where ContainerName=""" & domain & _ """ and DnsServerName=""" & DNSName & """ and DomainName=""" & domain & """") for each REC in Q MI=Rec.getMethodParameters("Modify") MI("TTL")=86400 MI("SerialNumber")=Clng(right("0000"&Year(now),4) & _ right("00" & month(now),2) & right("00" & day(now),2) & "01") MI("PrimaryServer")="your.primary.nameserver" MI("ResponsibleParty")="admin.yourdomain.com" MI("RefreshInterval")=86400 MI("RetryDelay")=7200 MI("ExpireLimit")=604800 MI("MinimumTTL")=86400 Rec.InvokeMethod("Modify",MI,Nothing) next ' delete NS records Q=QueryDNS("Select * From MicrosoftDNS_NSType where ContainerName=""" & domain & _ """ and DnsServerName=""" & DNSName & """and DomainName=""" & domain & """") for each REC in Q REC.delete next ' create new NS records MC=new ManagementClass(DNS,new ManagementPath("MicrosoftDNS_NSTYPE"),Nothing) Dim A1() as object={DNSName,domain,domain,1,86400,"your.primary.nameserver"} MC.InvokeMethod("CreateInstanceFromPropertyData",A1) A1(5)="your.secondary.nameserver" MC.InvokeMethod("CreateInstanceFromPropertyData",A1) ' create A Records MC=new ManagementClass(DNS,new ManagementPath("MicrosoftDNS_ATYPE"),Nothing) A1(2)="mail." & domain A1(5)="your IP address" MC.InvokeMethod("CreateInstanceFromPropertyData",A1) A1(2)="www." & domain MC.InvokeMethod("CreateInstanceFromPropertyData",A1) ' create MX-record MC=new ManagementClass(DNS,new ManagementPath("MicrosoftDNS_MXTYPE"),Nothing) Dim A2() as object={DNSName,domain,domain,1,86400,10,"mail." & domain} MC.InvokeMethod("CreateInstanceFromPropertyData",A2) End Sub '# '#opens a Query to the DNS-Server '# function QueryDNS(query as string) as ManagementObjectCollection Dim QS = new ManagementobjectSearcher( DNS,new ObjectQuery(query)) QueryDNS = QS.get() end function '# '#gets the first element of a Management object collection (results of a Query) '# function first(C as ManagementObjectCollection) as Object dim qe as ManagementObjectCollection.ManagementObjectEnumerator=C.GetEnumerator() qe.reset() if not (qe.movenext) then first = Nothing else first = qe.current() end if end function
    Sunday, May 30, 2004 7:06 PM

All replies

  • User1297008538 posted
    Cool sample! :)
    Monday, May 31, 2004 12:37 AM
  • User1145111801 posted
    HI all, does anyone know how to get this script to work with the DNS server in Windows 2003. I change the bits that need changing (DNS primary server etc) but I just keep getting a 'Generic Error' when trying to create the zone. Also , for the IPAddr in the code it equals 'A' but what is supposed to be the vaue of 'A' ? I've tried setting it to a string "" but it complains that it doesn't want a sring :-( Any DNS experts out there? Many thanks
    Wednesday, October 6, 2004 6:27 AM
  • User598145023 posted
    I'm no DNS expert but I've dealt with WMI quite a bit, some frequent problems are permission issues (especially if you're trying to call WMI remotely), what I suggest is that you get wmitools @ http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6430F853-1120-48DB-8CC5-F2ABDC3ED314  Install this tool and try browsing to the dns namespace and see if you can access it.

    They have a WMI browser that shows you the structure of the namespaces.  Maybe for microsoftdns the structure has changed for Server 2003.

    Let me know what you find because I'm starting this project now that might involve this type of thing.

    .NET Developer
    AW Systems Inc.
    Thursday, June 30, 2005 4:04 PM
  • User-2070978840 posted
    I'm having problems making this work.  I can connect with WMI tools myself just fine, but when I try running the script I get:

    "System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) "

    And it stops on "QueryDNS = QS.get()"

    Could someone tell me how I need to set permissons so that the asp.net account can do this?

    Thursday, July 7, 2005 1:01 PM
  • User598145023 posted
    when you run the .get command that's when credentials are tested, or in other words, that's when you are being authenticated. 

    To set permissions you need to go to Start--> Run --> wmimgmt.msc  (this should all be done on the accessed machine!!)
    The WMI window will open (it's always blank..)
    right click "WMI Control" --> Properties
    goto the security tab, open the Root folder, search for & select the appropriate namespace,
    click on the security button (bottom right), make sure "remote enable" is checked if you are trying to access
    via WMI remotely!!!

    try that.

    Noam Wolf
    .NET Developer
    Saturday, July 23, 2005 7:46 PM
  • User2084499974 posted
    alright, how do u modify the resource record? anyone?

    PS: has anyone ever thought to just create a DLL or a activex(or whatever its called now in vb.net) that does all these functions?
    I haven't seen anyone do one yet... just an idea :P
    Monday, August 1, 2005 5:44 PM
  • User1031506136 posted


    there are some changes made to dnsprov in win 2003, some of them are:

    1. If you are running the script on the same server then you shouldn't send username & pass to login.
    2. ChangeZoneType method have a bug related to ZoneType parameter to change the zonetype, microsoft is checking it & will be updated soon.
    3. There is a difference in Zonetype in dnsprov win 2003 that win 2000.
    4. There are new zone types & some changes to records

    Yes, there is component that performe the standard commands in dns and you can found it at www.aljazrawi.net , a new version that supports both windows 2000 & windows 2003 will be released soon but if you need a copy before its released just email me at info@aljazrawi.net & i'll send it to you.


    Tuesday, November 22, 2005 4:45 PM
  • User-2147312173 posted


    I have the same problem :Generic Failure error in  InvokeMethod("CreateZone",.,..).

    but i found that when run code from console application it work correctly but if i run it in a ASP.NET page it raise error Generic Failure.

    I think it relate to this fact that ASP.NET page run under Network Service user cridental  so i grant all access to Network Service user by wmimgmt.mcs tool bur problem is remained.

    any one have any idea?

    Thanks in advance for all of you.

    Tuesday, January 23, 2007 10:12 AM
  • User1593236550 posted

    I too am getting the error:

     Invalid parameter

    at the code:

    MC.InvokeMethod("CreateZone", MI, Nothing)

     Heeeeeeeeeeelp. This is doing my nut in...

    Monday, March 5, 2007 1:41 PM
  • User-1772065905 posted


     this code very cool sample but invokemethod not working. Do you do it ? Anybody else?


    Thursday, July 31, 2008 12:16 PM
  • User-914656897 posted

     Hi all;

    This is nice sample and it helped me alot but in NS record i also want to add IPAddress what should i do it is not adding any IP address to the NS record.IPAddress in Unknown there....any body can help ??


    Thanx in advance.

    Tuesday, January 27, 2009 12:49 AM
  • User-802687194 posted

    how to add virtual sub domain at runtime with folder name as subdomain.domain.com in our asp .net application in c#

    is there any help available....

    its urgent.............

    Wednesday, January 27, 2010 7:44 AM
  • User91062229 posted

    I know its an old thread, but maybe it will help someone in the future...

    Take a look at this project, it has a UI and a test project that shows how to create zones, manipulate and query Microsoft DNS with WMI.


    Thursday, May 24, 2012 7:39 AM