DNS update via code.

G

Guest

Hi,
I am trying to create a CName record on my DNS server. Seems like it
should be easy. I am using WMI. I have created a test asp.net page and put
the code in the code behind. The DNS server is a Windows 2000 box that sits
next to my pc. I have rights on the DNS box and use Remote desktop to admin
it. So here's the problem. When I try to run my code I get a "The RPC
server is unavailable." error.

Here is my code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Management;

public partial class DNSc : System.Web.UI.Page
{
ManagementScope DNS;
ManagementObject DNSSVR;

private string dnsname;
public string sResults = "";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
new_Zone("myDNSserver", "myDomain.com");
sResults = "DONE";
}
}

void new_Zone(string dnsserver, string domain)
{
ManagementClass MC;

// connect to WMI ****************************** connect to WMI
ConnectionOptions options = new ConnectionOptions();
options.Authentication =
System.Management.AuthenticationLevel.PacketPrivacy;
options.Username = "username";
options.Password = "password";

DNS = new ManagementScope("\\\\" + dnsserver +
"\\root\\microsoftdns",options);
DNSSVR = new ManagementObject(DNS, new
ManagementPath("MicrosoftDNS_Server.Name=\".\""), null);

DNSSVR.Get(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
dnsname = DNSSVR.Site.Name;

// connect to WMI ****************************** connect to WMI



// create MX-record ***************************** create MX-record
MC = new ManagementClass(DNS, new
ManagementPath("MicrosoftDNS_CNAMETYPE"), null);
object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
domain };
MC.InvokeMethod("CreateInstanceFromPropertyData", A2);
// create MX-record ***************************** create MX-record

}

}


The line where I receive my error is on the "Get()". I put a comment behind
it like this: // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Thank you for any help on this.

Michael.
 
W

Willy Denoyette [MVP]

see Inline

Willy.

| Hi,
| I am trying to create a CName record on my DNS server. Seems like it
| should be easy. I am using WMI. I have created a test asp.net page and
put
| the code in the code behind. The DNS server is a Windows 2000 box that
sits
| next to my pc. I have rights on the DNS box and use Remote desktop to
admin
| it. So here's the problem. When I try to run my code I get a "The RPC
| server is unavailable." error.
|
| Here is my code:
|
| using System;
| using System.Data;
| using System.Configuration;
| using System.Collections;
| using System.Web;
| using System.Web.Security;
| using System.Web.UI;
| using System.Web.UI.WebControls;
| using System.Web.UI.WebControls.WebParts;
| using System.Web.UI.HtmlControls;
| using System.Management;
|
| public partial class DNSc : System.Web.UI.Page
| {
| ManagementScope DNS;
| ManagementObject DNSSVR;
|
| private string dnsname;
| public string sResults = "";
|
| protected void Page_Load(object sender, EventArgs e)
| {
| if (!IsPostBack)
| {
| new_Zone("myDNSserver", "myDomain.com");
| sResults = "DONE";
| }
| }
|
| void new_Zone(string dnsserver, string domain)
| {
| ManagementClass MC;
|
| // connect to WMI ****************************** connect to WMI
| ConnectionOptions options = new ConnectionOptions();
| options.Authentication =
| System.Management.AuthenticationLevel.PacketPrivacy;
| options.Username = "username";
| options.Password = "password";
|
| DNS = new ManagementScope("\\\\" + dnsserver +
| "\\root\\microsoftdns",options);
| DNSSVR = new ManagementObject(DNS, new
| ManagementPath("MicrosoftDNS_Server.Name=\".\""), null);
|

1. The Name property cannot be used as such, the property must be a key,
which it is not (this class has no key type property).
You must use a select query instead to get an instance of your class.
2. The Name property will never hold a ., it holds the FQN of the DNS
server.

Following is bogus when used in above scenario, it's only required to be
called on a ManagementObjectSearcher like:

SelectQuery query = new SelectQuery("select Name from
MicrosoftDNS_Server");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(DNS, query, null))
{
foreach (ManagementObject mo in searcher.Get()) {
string dnsName = mo.Properties["Name"].ToString();
}


| DNSSVR.Get(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

What are you trying here?

Note that you need the DNS WMI extentions to be installed on W2K, I suggest
you first try to run your queries using Wbemetest.exe or WMIStudio (part of
the WMI SDK).




| dnsname = DNSSVR.Site.Name;
|
| // connect to WMI ****************************** connect to WMI
|
|
|
| // create MX-record ***************************** create MX-record
| MC = new ManagementClass(DNS, new
| ManagementPath("MicrosoftDNS_CNAMETYPE"), null);
| object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
| domain };
| MC.InvokeMethod("CreateInstanceFromPropertyData", A2);
| // create MX-record ***************************** create MX-record
|
| }
|
| }
|
|
| The line where I receive my error is on the "Get()". I put a comment
behind
| it like this: // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
| Thank you for any help on this.
|
| Michael.
|
 
G

Guest

Hi, I am sorry, but I am confused. I found this as sample code written in
VB.net and tried to convert it to C#. I just need a way to create a CName
record in my DNS. I am trying to allow users of my site to create a unique
domain...like michael.mysite.com

Do you now what I am doing wrong?




Willy Denoyette said:
see Inline

Willy.

| Hi,
| I am trying to create a CName record on my DNS server. Seems like it
| should be easy. I am using WMI. I have created a test asp.net page and
put
| the code in the code behind. The DNS server is a Windows 2000 box that
sits
| next to my pc. I have rights on the DNS box and use Remote desktop to
admin
| it. So here's the problem. When I try to run my code I get a "The RPC
| server is unavailable." error.
|
| Here is my code:
|
| using System;
| using System.Data;
| using System.Configuration;
| using System.Collections;
| using System.Web;
| using System.Web.Security;
| using System.Web.UI;
| using System.Web.UI.WebControls;
| using System.Web.UI.WebControls.WebParts;
| using System.Web.UI.HtmlControls;
| using System.Management;
|
| public partial class DNSc : System.Web.UI.Page
| {
| ManagementScope DNS;
| ManagementObject DNSSVR;
|
| private string dnsname;
| public string sResults = "";
|
| protected void Page_Load(object sender, EventArgs e)
| {
| if (!IsPostBack)
| {
| new_Zone("myDNSserver", "myDomain.com");
| sResults = "DONE";
| }
| }
|
| void new_Zone(string dnsserver, string domain)
| {
| ManagementClass MC;
|
| // connect to WMI ****************************** connect to WMI
| ConnectionOptions options = new ConnectionOptions();
| options.Authentication =
| System.Management.AuthenticationLevel.PacketPrivacy;
| options.Username = "username";
| options.Password = "password";
|
| DNS = new ManagementScope("\\\\" + dnsserver +
| "\\root\\microsoftdns",options);
| DNSSVR = new ManagementObject(DNS, new
| ManagementPath("MicrosoftDNS_Server.Name=\".\""), null);
|

1. The Name property cannot be used as such, the property must be a key,
which it is not (this class has no key type property).
You must use a select query instead to get an instance of your class.
2. The Name property will never hold a ., it holds the FQN of the DNS
server.

Following is bogus when used in above scenario, it's only required to be
called on a ManagementObjectSearcher like:

SelectQuery query = new SelectQuery("select Name from
MicrosoftDNS_Server");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(DNS, query, null))
{
foreach (ManagementObject mo in searcher.Get()) {
string dnsName = mo.Properties["Name"].ToString();
}


| DNSSVR.Get(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

What are you trying here?

Note that you need the DNS WMI extentions to be installed on W2K, I suggest
you first try to run your queries using Wbemetest.exe or WMIStudio (part of
the WMI SDK).




| dnsname = DNSSVR.Site.Name;
|
| // connect to WMI ****************************** connect to WMI
|
|
|
| // create MX-record ***************************** create MX-record
| MC = new ManagementClass(DNS, new
| ManagementPath("MicrosoftDNS_CNAMETYPE"), null);
| object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
| domain };
| MC.InvokeMethod("CreateInstanceFromPropertyData", A2);
| // create MX-record ***************************** create MX-record
|
| }
|
| }
|
|
| The line where I receive my error is on the "Get()". I put a comment
behind
| it like this: // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
| Thank you for any help on this.
|
| Michael.
|
 
W

Willy Denoyette [MVP]

| Hi, I am sorry, but I am confused. I found this as sample code written in
| VB.net and tried to convert it to C#. I just need a way to create a CName
| record in my DNS. I am trying to allow users of my site to create a
unique
| domain...like michael.mysite.com
|
| Do you now what I am doing wrong?
|
|
Hmm... I told you what was wrong, did you correct your code as I suggested?
Did you notice the suggestion to read the WMI docs and experiment using
wbemtest.exe , before trying to code in C# or whatever language, it will
save you a lot of time. Also I'm not clear why your users are allowed to
create DNS domain names, this is quite a privilege task, don't you think so?

Willy.
 
G

Guest

Well, I don't actually let them create a DNS domain name. When they sign up
on my site (which they can only do once), it creates a domain for them. They
really don't have any control over it. But I wan't it to be automatic. As
far as what you told me, I was and am confussed by what you told me. Could
you please clear it up for me?

Thanks,
Michael
 
G

Guest

Also,
I did add in the code you sent me, but on the Searcher.Get() I get the
following error: "The RPC server is unavailable. (Exception from HRESULT:
0x800706BA)"
 
G

Guest

When I looked up ManagementObjectSearcher.Get Method () , I found this
information:

Full trust for the immediate caller. This member cannot be used by partially
trusted code. For more information, see Using Libraries from Partially
Trusted Code.


Whould this cause the error to be thrown?
 
W

Willy Denoyette [MVP]

| Also,
| I did add in the code you sent me, but on the Searcher.Get() I get the
| following error: "The RPC server is unavailable. (Exception from HRESULT:
| 0x800706BA)"
|
|
|


Guess you have a firewall and/or a DCOM issue.
If you are running Microsoft's Firewall, you need to make sure that Remote
Administration is enabled at the remote firewall (if any) and at the local
firewall (if any) when using asynchronous WMI calls.
You need to make sure that both sides have port 135 open.
You need to make sure DCOM is configured appropriately.
Please consult MSDN for details (search for "Connecting through windows
firewall" in the WMI SDK).

Willy.
 
W

Willy Denoyette [MVP]

| When I looked up ManagementObjectSearcher.Get Method () , I found this
| information:
|
| Full trust for the immediate caller. This member cannot be used by
partially
| trusted code. For more information, see Using Libraries from Partially
| Trusted Code.
|
|
| Whould this cause the error to be thrown?
|

Nope, see my previous replay.

Willy.
 
W

Willy Denoyette [MVP]

| Well, I don't actually let them create a DNS domain name. When they sign
up
| on my site (which they can only do once), it creates a domain for them.
They
| really don't have any control over it. But I wan't it to be automatic.
As
| far as what you told me, I was and am confussed by what you told me.
Could
| you please clear it up for me?
|


Well, what I told you is basic WMI stuff, you can't create an instance of a
class using the following:

DNSSVR = new ManagementObject(DNS, new
ManagementPath("MicrosoftDNS_Server.Name=\".\""), null);

this requires 'Name' to be a key property, which it is NOT (none of the
class properties is a key).
That means that you have to use a select query as I showed you. Another
point was that you shouldn't expect the Name property to be ".", Name will
hold the FQN of the DNS server.
That means that not only the wrong constructor is used to return the
instance, but also the key value (Name='.') can never exists, so, even if
Name was a key, there would never exist such an instance.

If you run this code you will see what I mean:
// setup your scope
...
SelectQuery query = new SelectQuery("select Name from
MicrosoftDNS_Server");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query, null))
{
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine(mo["Name"].ToString());
}
}
...

But again, before you start coding, take a look at wbemtest.exe, and run
your queries from this tool, it will save you a lot of time once you start
coding, and it's also a great learning tool.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top