By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

DNS update via code.

P: n/a
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.PacketPrivac y;
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.

Sep 25 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
see Inline

Willy.

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:48**********************************@microsof t.com...
| 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.PacketPrivac y;
| 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.
|
Sep 25 '06 #2

P: n/a
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 [MVP]" wrote:
see Inline

Willy.

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:48**********************************@microsof t.com...
| 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.PacketPrivac y;
| 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.
|
Sep 25 '06 #3

P: n/a

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:47**********************************@microsof t.com...
| 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.


Sep 25 '06 #4

P: n/a
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

"Willy Denoyette [MVP]" wrote:
>
"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:47**********************************@microsof t.com...
| 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.


Sep 25 '06 #5

P: n/a
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)"

"Willy Denoyette [MVP]" wrote:
>
"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:47**********************************@microsof t.com...
| 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.


Sep 25 '06 #6

P: n/a
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?


"Willy Denoyette [MVP]" wrote:
>
"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:47**********************************@microsof t.com...
| 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.


Sep 25 '06 #7

P: n/a

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
| 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.
Sep 26 '06 #8

P: n/a

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:43**********************************@microsof t.com...
| 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.


Sep 26 '06 #9

P: n/a

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:39**********************************@microsof t.com...
| 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.

Sep 26 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.