Ghooti Farangi
Ghooti Farangi

Reputation: 20196

Create a DNS NSRecord in C#

I try to create a DNS NSRecord by DNS WMI Classes. The below is my sample code.

How can I set an IP address for a NSRecord? I know Enter the IP is required for a NSRecord.

    ManagementScope WmiScope = new ManagementScope("\\\\" + System.Environment.MachineName + "\\ROOT\\MicrosoftDNS");
ManagementPath path = new ManagementPath("MicrosoftDNS_NSType");
ManagementClass zone = new ManagementClass(WmiScope, path, null);
ManagementBaseObject p = zone.GetMethodParameters("CreateInstanceFromPropertyData");


p.Properties["DnsServerName"].Value = WmiScope.Path.Server;
p.Properties["ContainerName"].Value = "mydomain.com";
p.Properties["OwnerName"].Value = "";
p.Properties["NSHost"].Value = "ns1.domain.com";
zone.InvokeMethod("CreateInstanceFromPropertyData", p, null);

Upvotes: 1

Views: 1844

Answers (3)

RiptoR
RiptoR

Reputation: 466

I realise this is a very old post (4+ years old), but there is a way to add IP addresses for nameservers with WMI. As JayRO-GreyBeard above me states, you'll need to add a glue record for each nameserver.

A glue record is a Resource Record, and you can add it with 'CreateInstanceFromTextRepresentation' (the management path is 'MicrosoftDNS_ResourceRecord').

The 'TextRepresentation' you need to use for a glue record is '{hostname} IN A {IP}'. For example: 'ns1.example.com. IN A 127.0.0.1'. As the example show, make sure you end the hostname with a dot ('.').

ManagementClass objMC_RRecords = new ManagementClass(objScope, new ManagementPath("MicrosoftDNS_ResourceRecord"), null);

ManagementBaseObject objParams_RR_NS1 = objMC_RRecords.GetMethodParameters("CreateInstanceFromTextRepresentation");
objParams_RR_NS1["DnsServerName"] = null;
objParams_RR_NS1["ContainerName"] = "example.com";
objParams_RR_NS1["TextRepresentation"] = "ns1.example.com. IN A 127.0.0.1";

objMC_RRecords.InvokeMethod("CreateInstanceFromTextRepresentation", objParams_RR_NS1, null);

More info on the 'CreateInstanceFromTextRepresentation' method can be found here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682714(v=vs.85).aspx

Upvotes: 0

JayRO-GreyBeard
JayRO-GreyBeard

Reputation: 149

Old thread, but I am wrestling with this issue, maybe this will help someone.

While the IPAddress for the NSRecord is not required, Microsoft DNS will in fact create a host (A) glue record for that NSRecord. By having a glue record in the delegated zone, a step is saved (slightly quicker) in not having to look up the address of that NSRecord.

Sadly, having wasted a couple of hours now, there seems to be no method for adding a glue record using WMI.

Upvotes: 0

Anthony Shaw
Anthony Shaw

Reputation: 8166

Technically an NS record for a domain does not contain an IP address, it is the authroitative record telling "anybody" requesting the NS record, all of the NS records for the domain.

An NS-record identifies the name of a DNS server - not the IP-address.

http://www.mtgsy.net/dns/record_ns.htm

Upvotes: 1

Related Questions