Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an SRV record in DNS with C#

Tags:

c#

dns

wmi

srv

I am using WMI to create different types of DNS records but am having a problem with SRV records. I keep getting a "Not found" error whenever I pass the DomainName parameter. The domain name looks good to me.

Has anyone ever successfully done this?

Here is my code:

internal static void CreateSrvRecordInDns(string Zone, string OwnerName, string DomainName, UInt16 Weight, UInt16 Priority, UInt16 Port)
    {
        DnsProvider dns = new DnsProvider();
        ManagementClass mClass = new ManagementClass(dns.Session, new ManagementPath("MicrosoftDNS_SrvType"), null);
        ManagementBaseObject inParams = mClass.GetMethodParameters("CreateInstanceFromPropertyData");
        inParams["DnsServerName"] = dns.Server;
        inParams["ContainerName"] = Zone;
        inParams["OwnerName"] = OwnerName;
        inParams["DomainName"] = DomainName; //Error occurs here
        inParams["Port"] = Port;
        inParams["Priority"] = Priority;
        inParams["Weight"] = Weight;
        mClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
        dns.Dispose(ref inParams);
        dns.Dispose(ref mClass);
    }
like image 849
mcass20 Avatar asked Sep 22 '10 18:09

mcass20


1 Answers

Simply replace the problematic line with:

inParams["SRVDomainName"] = DomainName;

I don't know the reason, but when got the properties list by:

PropertyData[] pd = new PropertyData[inParams.Properties.Count];
inParams.Properties.CopyTo(pd,0);

This was the name of this field (Microsoft's bug?)

HTH.

P.S. In order to see the right format for each field, use wbemtest tool (wbemtest from command prompt), connect to root\MicrosoftDNS namespace and run the following query:

Select * from MicrosoftDNS_SRVType

You should use the same format as the instances listed in the answer).

like image 50
rkellerm Avatar answered Sep 21 '22 05:09

rkellerm