Search code examples
c#dnswmi

Using WMI to create active directory dns zones


I'm using the following code to create a DNS zone:

ManagementClass zoneObj = new ManagementClass(session, new ManagementPath("MicrosoftDNS_Zone"), null);
ManagementBaseObject inParams = zoneObj.GetMethodParameters("CreateZone");
inParams["ZoneName"] = "thedomain.com";
inParams["ZoneType"] = 0;
ManagementBaseObject outParams = zoneObj.InvokeMethod("CreateZone", inParams, null);

This creates the zone, but creates it with the type "Standard Primary". I need it to create with the type "Active Directory - Integrated Primary". From all my research, the zone type of "0" should do this. Can anyone tell me why it won't create the zone as an active directory zone?


Solution

  • I figured it out. Anyone else with the same issue, you have to add the DsIntegrated parameter to tell it to use Active Directory. Here is the final code:

    ManagementClass zoneObj = new ManagementClass(session, new ManagementPath("MicrosoftDNS_Zone"), null);
    ManagementBaseObject inParams = zoneObj.GetMethodParameters("CreateZone");
    inParams["ZoneName"] = "thedomain.com";
    inParams["ZoneType"] = 0;
    inParams["DsIntegrated"] = true; //--- this is what needed to be added
    ManagementBaseObject outParams = zoneObj.InvokeMethod("CreateZone", inParams, null);
    

    DsIntegrated Indicates whether zone data is stored in the Active Directory or in files. If TRUE, the data is stored in the Active Directory; if FALSE, the data is stored in files.