DNS update via code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    DNS update via code.

    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.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Manageme nt;

    public partial class DNSc : System.Web.UI.P age
    {
    ManagementScope DNS;
    ManagementObjec t DNSSVR;

    private string dnsname;
    public string sResults = "";

    protected void Page_Load(objec t sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    new_Zone("myDNS server", "myDomain.com") ;
    sResults = "DONE";
    }
    }

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

    // connect to WMI *************** *************** connect to WMI
    ConnectionOptio ns options = new ConnectionOptio ns();
    options.Authent ication =
    System.Manageme nt.Authenticati onLevel.PacketP rivacy;
    options.Usernam e = "username";
    options.Passwor d = "password";

    DNS = new ManagementScope ("\\\\" + dnsserver +
    "\\root\\micros oftdns",options );
    DNSSVR = new ManagementObjec t(DNS, new
    ManagementPath( "MicrosoftDNS_S erver.Name=\".\ ""), null);

    DNSSVR.Get(); // !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!
    dnsname = DNSSVR.Site.Nam e;

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



    // create MX-record *************** ************** create MX-record
    MC = new ManagementClass (DNS, new
    ManagementPath( "MicrosoftDNS_C NAMETYPE"), null);
    object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
    domain };
    MC.InvokeMethod ("CreateInstanc eFromPropertyDa ta", 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.

  • Willy Denoyette [MVP]

    #2
    Re: DNS update via code.

    see Inline

    Willy.

    "Michael" <Michael@discus sions.microsoft .comwrote in message
    news:4838F04C-609D-4CE8-9A97-C9CBCD0084C8@mi crosoft.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.Configur ation;
    | using System.Collecti ons;
    | using System.Web;
    | using System.Web.Secu rity;
    | using System.Web.UI;
    | using System.Web.UI.W ebControls;
    | using System.Web.UI.W ebControls.WebP arts;
    | using System.Web.UI.H tmlControls;
    | using System.Manageme nt;
    |
    | public partial class DNSc : System.Web.UI.P age
    | {
    | ManagementScope DNS;
    | ManagementObjec t DNSSVR;
    |
    | private string dnsname;
    | public string sResults = "";
    |
    | protected void Page_Load(objec t sender, EventArgs e)
    | {
    | if (!IsPostBack)
    | {
    | new_Zone("myDNS server", "myDomain.com") ;
    | sResults = "DONE";
    | }
    | }
    |
    | void new_Zone(string dnsserver, string domain)
    | {
    | ManagementClass MC;
    |
    | // connect to WMI *************** *************** connect to WMI
    | ConnectionOptio ns options = new ConnectionOptio ns();
    | options.Authent ication =
    | System.Manageme nt.Authenticati onLevel.PacketP rivacy;
    | options.Usernam e = "username";
    | options.Passwor d = "password";
    |
    | DNS = new ManagementScope ("\\\\" + dnsserver +
    | "\\root\\micros oftdns",options );
    | DNSSVR = new ManagementObjec t(DNS, new
    | ManagementPath( "MicrosoftDNS_S erver.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 ManagementObjec tSearcher like:

    SelectQuery query = new SelectQuery("se lect Name from
    MicrosoftDNS_Se rver");
    using(Managemen tObjectSearcher searcher = new
    ManagementObjec tSearcher(DNS, query, null))
    {
    foreach (ManagementObje ct 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.Nam e;
    |
    | // connect to WMI *************** *************** connect to WMI
    |
    |
    |
    | // create MX-record *************** ************** create MX-record
    | MC = new ManagementClass (DNS, new
    | ManagementPath( "MicrosoftDNS_C NAMETYPE"), null);
    | object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
    | domain };
    | MC.InvokeMethod ("CreateInstanc eFromPropertyDa ta", 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.
    |


    Comment

    • Michael

      #3
      Re: DNS update via code.

      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" <Michael@discus sions.microsoft .comwrote in message
      news:4838F04C-609D-4CE8-9A97-C9CBCD0084C8@mi crosoft.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.Configur ation;
      | using System.Collecti ons;
      | using System.Web;
      | using System.Web.Secu rity;
      | using System.Web.UI;
      | using System.Web.UI.W ebControls;
      | using System.Web.UI.W ebControls.WebP arts;
      | using System.Web.UI.H tmlControls;
      | using System.Manageme nt;
      |
      | public partial class DNSc : System.Web.UI.P age
      | {
      | ManagementScope DNS;
      | ManagementObjec t DNSSVR;
      |
      | private string dnsname;
      | public string sResults = "";
      |
      | protected void Page_Load(objec t sender, EventArgs e)
      | {
      | if (!IsPostBack)
      | {
      | new_Zone("myDNS server", "myDomain.com") ;
      | sResults = "DONE";
      | }
      | }
      |
      | void new_Zone(string dnsserver, string domain)
      | {
      | ManagementClass MC;
      |
      | // connect to WMI *************** *************** connect to WMI
      | ConnectionOptio ns options = new ConnectionOptio ns();
      | options.Authent ication =
      | System.Manageme nt.Authenticati onLevel.PacketP rivacy;
      | options.Usernam e = "username";
      | options.Passwor d = "password";
      |
      | DNS = new ManagementScope ("\\\\" + dnsserver +
      | "\\root\\micros oftdns",options );
      | DNSSVR = new ManagementObjec t(DNS, new
      | ManagementPath( "MicrosoftDNS_S erver.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 ManagementObjec tSearcher like:
      >
      SelectQuery query = new SelectQuery("se lect Name from
      MicrosoftDNS_Se rver");
      using(Managemen tObjectSearcher searcher = new
      ManagementObjec tSearcher(DNS, query, null))
      {
      foreach (ManagementObje ct 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.Nam e;
      |
      | // connect to WMI *************** *************** connect to WMI
      |
      |
      |
      | // create MX-record *************** ************** create MX-record
      | MC = new ManagementClass (DNS, new
      | ManagementPath( "MicrosoftDNS_C NAMETYPE"), null);
      | object[] A2 = { dnsname, domain, domain, 1, 86400, 10, "test." +
      | domain };
      | MC.InvokeMethod ("CreateInstanc eFromPropertyDa ta", 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.
      |
      >
      >
      >

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: DNS update via code.




        "Michael" <Michael@discus sions.microsoft .comwrote in message
        news:47A440EE-302C-45D0-8B22-16DFA18A663E@mi crosoft.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.




        Comment

        • Michael

          #5
          Re: DNS update via code.

          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" <Michael@discus sions.microsoft .comwrote in message
          news:47A440EE-302C-45D0-8B22-16DFA18A663E@mi crosoft.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.
          >
          >
          >
          >
          >

          Comment

          • Michael

            #6
            Re: DNS update via code.

            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" <Michael@discus sions.microsoft .comwrote in message
            news:47A440EE-302C-45D0-8B22-16DFA18A663E@mi crosoft.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.
            >
            >
            >
            >
            >

            Comment

            • Michael

              #7
              Re: DNS update via code.

              When I looked up ManagementObjec tSearcher.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" <Michael@discus sions.microsoft .comwrote in message
              news:47A440EE-302C-45D0-8B22-16DFA18A663E@mi crosoft.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.
              >
              >
              >
              >
              >

              Comment

              • Willy Denoyette [MVP]

                #8
                Re: DNS update via code.


                "Michael" <Michael@discus sions.microsoft .comwrote in message
                news:150CD2DA-033B-4F12-BBEC-A1D800577A46@mi crosoft.com...
                | When I looked up ManagementObjec tSearcher.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.


                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: DNS update via code.


                  "Michael" <Michael@discus sions.microsoft .comwrote in message
                  news:43ECCACC-0C5F-44D9-B669-9D586E1AC751@mi crosoft.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.




                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: DNS update via code.


                    "Michael" <Michael@discus sions.microsoft .comwrote in message
                    news:39286561-EC3E-4AF5-BDFF-913546D235AE@mi crosoft.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 ManagementObjec t(DNS, new
                    ManagementPath( "MicrosoftDNS_S erver.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("se lect Name from
                    MicrosoftDNS_Se rver");
                    using(Managemen tObjectSearcher searcher = new
                    ManagementObjec tSearcher(scope , query, null))
                    {
                    foreach (ManagementObje ct mo in searcher.Get()) {
                    Console.WriteLi ne(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.





                    Comment

                    Working...