Re: Programmatically create a DNS Zone/File in C#
From: William Stacey (staceywREMOVE_at_mvps.org)
Date: 02/09/04
- Next message: William Stacey: "Re: DNS lookup failures"
- Previous message: William Stacey: "Re: DNS Root Hints / NSLookup"
- In reply to: Luis Cabrera: "Programmatically create a DNS Zone/File in C#"
- Next in thread: Luis Cabrera: "Re: Programmatically create a DNS Zone/File in C#"
- Reply: Luis Cabrera: "Re: Programmatically create a DNS Zone/File in C#"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 9 Feb 2004 16:01:08 -0500
I don't know of any sample of this, other then my own. Have not looked at
this in a while, so you may need to tweek it - this is w2k3 specific. For
w2k, I have another version for ya if you need. This should get you
started, however. Then your off to the races when you get the feel. HTH
--
William Stacey, MVP
public override void CreateZone(string zoneName, ZoneTypes zoneType, bool
adIntegrated, string dataFileName, IPAddress[] ipAddresses, string
adminEmailName)
{
// Parameters
// zoneName
// [in, required] String representing the name of the zone. Exception if
null or evaluates to empty string.
// zoneType
// [in, required] W2K3ZoneTypes representing the type of zone. Valid
values are:
// 0 - Primary, 1 - Secondary, 2 - Stub, 3 - Forward
// dsIntegrated
// [in, optional] Bool representing if this zone should be AD-Integrated.
True is valid only if primary zone.
// If null, default of not dsIntegrated is used.
// dataFileName
// [in, optional] Name of the data file to use to create the zone (i.e.
import file.) If null, file is created with default name.
// ipAddresses
// [in, optional] IP address array of the master DNS Server(s) for the
zone. Required for Secondary zone type, otherwise ignored.
// AdminEmailName
// [in, optional] Email address of the administrator responsible for the
zone. If null, default is used.
//Step 1 - Validate Input
zoneName = Globals.NullToEmptyTrim(zoneName);
adminEmailName = Globals.NullToEmptyTrim(adminEmailName);
dataFileName = Globals.NullToEmptyTrim(dataFileName);
if ( zoneName == "" )
throw new ArgumentNullException("zoneName is null or empty.");
if ( (uint)zoneType > 3 )
throw new ArgumentOutOfRangeException("zoneType out of range.");
string[] ipAddressesArray = null; // string array of string IP addresses
used by WMI.
switch( zoneType )
{
case ZoneTypes.Primary:
ipAddressesArray = null; // Ignore any ipAddresses as not valid for a
primary zone.
if ( adIntegrated )
dataFileName = null; // Ignore any datafilename for dsintegrated zones
to avoid error in WMI.
// Note: if user supplies dataFileName, it must exist or DNS API will
throw error.
// Otherwise, API will create name using default of zone name + ".dns".
break;
case ZoneTypes.Secondary:
if ( ipAddresses == null || ipAddresses.Length == 0 )
throw new ArgumentException("Secondary zones must have primary IPAddress
array.");
ipAddressesArray = Globals.IPAddressArrayToStringArray(ipAddresses);
adIntegrated = false; // Ignore dsIntegrated for secondary zones as not
valid.
// For secondary zones, we ignore any dataFileName and use default name,
// which is zone name with a suffix of ".dns". GUI does not ask and uses
default.
dataFileName = zoneName + ".dns";
adminEmailName = null; // Ignore adminEmailName for Secondary zone.
break;
case ZoneTypes.Stub:
if ( ipAddresses == null || ipAddresses.Length == 0 )
throw new ArgumentException("Stub zone must have a primary IPAddress
array.");
ipAddressesArray = Globals.IPAddressArrayToStringArray(ipAddresses);
adIntegrated = false; // Ignore dsIntegrated for stub zones as not
supported.
// Note, we could allow user to pick a file name, but prevent to keep
simple.
dataFileName = zoneName + ".dns"; //ignore any file name passed and use
default.
adminEmailName = null; // Ignore adminEmailName for Stub zone.
break;
case ZoneTypes.Forward:
if ( ipAddresses == null || ipAddresses.Length == 0 )
throw new ArgumentException("Forward zone must have a primary IPAddress
array.");
ipAddressesArray = Globals.IPAddressArrayToStringArray(ipAddresses);
adIntegrated = false; // Ignore dsIntegrated for other zone types.
dataFileName = null; // Ignore any dataFileName as not valid for forward
zones. WMI would ignore anyway.
adminEmailName = null; // Ignore adminEmailName for Forward zone.
break;
default:
throw new ArgumentOutOfRangeException("zoneType not valid.");
}
//Step 2 - Create the zone.
try
{
InvokeMethodOptions options = new InvokeMethodOptions();
options.Timeout = new TimeSpan(0, 0, 0, this.timeout);
ManagementClass zoneClass = new ManagementClass(
ms,
new ManagementPath("MicrosoftDNS_Zone"),
null);
//Get an input parameters object for this method
ManagementBaseObject inParams =
zoneClass.GetMethodParameters("CreateZone");
//Fill in input parameter values
inParams["ZoneName"] = zoneName;
inParams["ZoneType"] = (uint)zoneType;
inParams["DsIntegrated"] = adIntegrated;
inParams["DataFileName"] = dataFileName;
inParams["IpAddr"] = ipAddressesArray;
inParams["AdminEmailName"] = adminEmailName;
//Execute the method
ManagementBaseObject outObject = zoneClass.InvokeMethod ("CreateZone",
inParams, options);
}
catch(ManagementException e)
{
throw new ApplicationException("WMI method exception. Check
innerException for details.", e);
}
catch(Exception e)
{
throw new ApplicationException("CreateZone method Application Exception.
Check innerException for details.", e);
}
} // End CreateZone()
- Next message: William Stacey: "Re: DNS lookup failures"
- Previous message: William Stacey: "Re: DNS Root Hints / NSLookup"
- In reply to: Luis Cabrera: "Programmatically create a DNS Zone/File in C#"
- Next in thread: Luis Cabrera: "Re: Programmatically create a DNS Zone/File in C#"
- Reply: Luis Cabrera: "Re: Programmatically create a DNS Zone/File in C#"
- Messages sorted by: [ date ] [ thread ]