DNS administration in PowerShell, including tasks like creating zones and adding Host (A) records, can be performed using the WMI interface. Full documentation for the interface is available from Microsoft in the DNS WMI Provider Reference.

I have released a PowerShell 2.0 module using the WMI provider here.

There are a few limitations of the interface. The properties associated with Aging are read-only and cannot be set. Several of the configuration options are not available including the option to enable GlobalNames with Windows Server 2008.

Common variables

The examples below use two common variables. Both should be updated to reflect the environment used to execute any command.

Management Class vs Management Object

Two different classes from the .NET Framework are used below. The ManagementObject, created using Get-WMIObject, and the ManagementClass created using [WMIClass].

[WMIClass] creates the same object as the following example.

In general terms, to change something that already exists use the properties and methods of a ManagementObject via Get-WMIObject. To add something new use the methods associated with a ManagementClass.

Creating an instance of a Management Object

Exploring the Management Object

Showing the object type:
[code lang="plain"]
PS C:\> $Zones.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

Showing the properties and methods:
[code lang="plain"]
PS C:\> $Zones | Get-Member

TypeName: System.Management.ManagementObject#root\MicrosoftDNS\MicrosoftDNS_Zone

Name MemberType Definition
---- ---------- ----------
AgeAllRecords Method Management.ManagementBaseObject AgeAllRec...
ChangeZoneType Method Management.ManagementBaseObject ChangeZon...
ForceRefresh Method Management.ManagementBaseObject ForceRefr...
GetDistinguishedName Method Management.ManagementBaseObject GetDistin...
PauseZone Method Management.ManagementBaseObject PauseZone()
ReloadZone Method Management.ManagementBaseObject ReloadZone()
ResetSecondaries Method Management.ManagementBaseObject ResetSeco...
ResumeZone Method Management.ManagementBaseObject ResumeZone()
UpdateFromDS Method Management.ManagementBaseObject UpdateFro...
WriteBackZone Method Management.ManagementBaseObject WriteBack...
Aging Property Boolean Aging {get;set;}
AllowUpdate Property UInt32 AllowUpdate {get;set;}
AutoCreated Property Boolean AutoCreated {get;set;}
AvailForScavengeTime Property UInt32 AvailForScavengeTime {get;set;}
Caption Property String Caption {get;set;}
ContainerName Property String ContainerName {get;set;}
DataFile Property String DataFile {get;set;}
Description Property String Description {get;set;}
DisableWINSRecordReplicat Property Boolean DisableWINSRecordReplication {ge...
DnsServerName Property String DnsServerName {get;set;}
DsIntegrated Property Boolean DsIntegrated {get;set;}
ForwarderSlave Property Boolean ForwarderSlave {get;set;}
ForwarderTimeout Property UInt32 ForwarderTimeout {get;set;}
InstallDate Property String InstallDate {get;set;}
LastSuccessfulSoaCheck Property UInt32 LastSuccessfulSoaCheck {get;set;}
LastSuccessfulXfr Property UInt32 LastSuccessfulXfr {get;set;}
LocalMasterServers Property String[] LocalMasterServers {get;set;}
MasterServers Property String[] MasterServers {get;set;}
Name Property String Name {get;set;}
NoRefreshInterval Property UInt32 NoRefreshInterval {get;set;}
Notify Property UInt32 Notify {get;set;}
NotifyServers Property String[] NotifyServers {get;set;}
Paused Property Boolean Paused {get;set;}
RefreshInterval Property UInt32 RefreshInterval {get;set;}
Reverse Property Boolean Reverse {get;set;}
ScavengeServers Property String[] ScavengeServers {get;set;}
SecondaryServers Property String[] SecondaryServers {get;set;}
SecureSecondaries Property UInt32 SecureSecondaries {get;set;}
Shutdown Property Boolean Shutdown {get;set;}
Status Property String Status {get;set;}
UseNBStat Property Boolean UseNBStat {get;set;}
UseWins Property Boolean UseWins {get;set;}
ZoneType Property UInt32 ZoneType {get;set;}
...

Showing a subset of the properties within the object:
[code lang="plain"]
PS C:\> $Zones | Select-Object Name, DsIntegrated, ZoneType, Reverse

Name DsIntegrated ZoneType Reverse
---- ------------ -------- -------
1.2.3.in-addr.arpa False 1 True
1.2.4.in-addr.arpa False 1 True
1.2.5.in-addr.arpa False 1 True

Creating an instance of a Management Class

Exploring the Management Class

Showing the object type:
[code lang="plain"]
PS C:\> $ZoneClass.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ManagementClass System.Management.ManagementObject

Showing the properties and methods:
[code lang="plain"]
PS C:\> $ZoneClass | Get-Member

TypeName: System.Management.ManagementClass#ROOT\MicrosoftDNS\MicrosoftDNS_Zone

Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = __Class
CreateZone Method System.Management.ManagementBaseObject CreateZone(Sys...
...

Listing the parameters required for a method

The output above truncates the strings detailing the parameters used for each method. The following shows how the full list can be displayed.

Note that while this shows the parameter type it does not show whether or not the parameter is required or optional. For full details refer to the DNS WMI Provider Reference.

Creating Zones

The following values represent the Zone Types available with Microsoft DNS.
[code lang="plain"]
0 Primary zone
1 Secondary zone
2 Stub zone
* Windows Server 2003: This zone type is introduced in Windows Server 2003.
3 Zone forwarder
* Windows Server 2003: This zone type is introduced in Windows Server 2003.

Create a Forward or Reverse Lookup Zone

This example shows all of the possible parameters, this can be reduced to a single line by dropping the comments and use of variables. Note that any optional variable can be set to $Null or "".

Examples

Creating resource records with CreateInstanceFromPropertyData

CreateInstanceFromPropertyData is available on each individual record class. For example, the method can be invoked from MicrosoftDNS_AType, or MicrosoftDNS_MXType, and so on. Note that the syntax for the method varies slightly depending on the record type.

Create an A record

Create an MX record

Creating resource records with CreateInstanceFromTextRepresentation

CreateInstanceFromTextRepresentation is available from the MicrosoftDNS_ResourceRecord class. It takes fewer parameters than the previous method but ultimately requires exactly the same information.

Create an NS record

Create a TXT record

Update server data file

DNS zones are held in memory, any change to the zone is performed in memory rather than as a direct alteration of the zone file. The following method can be used to force the updated zone to write back to the file.

Reload a zone

Changes made to the zone file can be loaded into memory immediately using the ReloadZone method.

Enabling and starting scavenging on a server

Enabling scavenging requires setting the ScavengingInterval property to a non-zero value. The value representing the interval uses Hours.

Clear the cache

Any cached entries on a server can be cleared using the ClearCache method of the MicrosoftDNS_Cache class.

Displaying the DNS server statistics

Each DNS server holds a variety of statistics that can help to evaluate server performance.

p5rn7vb

6 Comments

  1. Thank! This post have been very helpful!

    Reply

  2. This is a great reference. Thank you very much!

    One of the things that I really need to automate is configuring zone transfers. I maintain quite a few DNS domains and I typically need to update the zone transfer information on all of them at the same time. Is there a way to do that via PowerShell (manage zone transfer information)?

    Thanks,
    David

    Reply

  3. Found help and knowledge in your article.

    Great one! Thanks!

    ~ Per Leon

    Reply

  4. Thanks for the information. Very good! Do you know of any way to add name servers to a zone?

    Reply

    • Yep, you need to create NS records in the zone (I popped a section in the article for that). Once done your new servers will appear on the name servers tab.

      Chris

      Reply

  5. Pingback: PowerShell – Получаем список DNS Conditional Forwarders « ИТ Блог Алексея Максимова

  6. Hey mate,

    Thanks for this post! I used it, in combination with a ServerFault question I asked, to create a script to mass rename a bunch of old A records on our Active Directory/DNS server during a huge server move yesterday morning. My finished script is here:

    http://penguindreams.org/blog/reassigning-dns-entires-in-windowsactive-directory-using-powershell/

    Keep posting awesome stuff!

    Reply

  7. Pingback: Penguin Dreams » Blog Archive » Reassigning DNS Entries in Windows/Active Directory using Powershell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">