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.
<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
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 "1.2.3.4" but it complains that it doesn't want a sring :-( Any DNS experts out there? Many thanks
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!!!
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
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.
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.
CodeInTheBot...
Member
55 Points
11 Posts
Access the Microsoft DNS-Server using WMI
May 30, 2004 11:06 PM|LINK
<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 functionbdesmond
Contributor
4745 Points
944 Posts
MVP
Re: Access the Microsoft DNS-Server using WMI
May 31, 2004 04:37 AM|LINK
Windows Server MVP - Directory Services
http://www.briandesmond.com
bumbling foo...
Member
210 Points
42 Posts
Re: Access the Microsoft DNS-Server using WMI
Oct 06, 2004 10:27 AM|LINK
noamw
Member
175 Points
37 Posts
Re: Access the Microsoft DNS-Server using WMI
Jun 30, 2005 08:04 PM|LINK
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.
--noam
.NET Developer
AW Systems Inc.
.NET Developer
noamwolf.com
orphicfireba...
Member
485 Points
111 Posts
Re: Access the Microsoft DNS-Server using WMI
Jul 07, 2005 05:01 PM|LINK
"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?
noamw
Member
175 Points
37 Posts
Re: Access the Microsoft DNS-Server using WMI
Jul 23, 2005 11:46 PM|LINK
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.
best,
Noam Wolf
.NET Developer
noamwolf.com
.NET Developer
noamwolf.com
mattboyy
Member
5 Points
1 Post
Re: Access the Microsoft DNS-Server using WMI
Aug 01, 2005 09:44 PM|LINK
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
Gentleman33
Member
5 Points
1 Post
Re: Access the Microsoft DNS-Server using WMI
Nov 22, 2005 08:45 PM|LINK
hi,
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.
Regards,
Sarmad
www.aljazrawi.net
mfarshadmehr
Member
279 Points
151 Posts
Re: Access the Microsoft DNS-Server using WMI
Jan 23, 2007 02:12 PM|LINK
Hi,
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.
metamind
Member
137 Points
32 Posts
Re: Access the Microsoft DNS-Server using WMI
Mar 05, 2007 05:41 PM|LINK
I too am getting the error:
Invalid parameter
at the code:
MC.InvokeMethod("CreateZone", MI,
Nothing)Heeeeeeeeeeelp. This is doing my nut in...