Powershell module for DNS

DNS to change a dynamic record to a static record



DNS PowerShell – import-module DNSServer

example: http://www.rivnet.ro/category/active-directory/dns


DNS Powershell – DnsShell

 Summary: Learn how to use a free Windows PowerShell module to ease administration of Windows DNS: DnsShell (http://dnsshell.codeplex.com/)

What is DnsShell?

The majority of the cmdlets in DnsShell are wrappers around the WMI interface. The WMI interface tends to be fairly difficult to work with, or at least more difficult than it needs to be. For the most part this is due to the infamous Generic Error it returns whenever something goes wrong.

In addition to the WMI wrappers, DnsShell contains an interface for working with DNS via LDAP with decoders for the dnsProperty and dnsRecord attributes.

The final cmdlet, Get-Dns, is a DNS resolver, designed to exceed the capabilities of nslookup and to be comparable with Dig.

This post aims to explore some of the capabilities for DnsShell, based on the tasks I use it for on a regular basis.

Basic tasks

Listing zones

Get-DnsZone can be used to return information about zones configured on a server. This cmdlet uses WMI to grab details of the zone. The parameters used with this cmdlet are used to
build a WQL filter.

# All zones


# Primary zones

Get-DnsZone -ZoneType Primary | Format-List

Zone information can be returned from Active Directory using Get-ADDnsZone. By default, Get-ADDnsZone targets the DomainDnsZones application partition.

The information returned by this cmdlet differs slightly from Get-DnsRecord, only showing detail stored in the dnsProperty attribute.

# Returning all zones in DomainDnsZoness (for the current domain)


# Returning all zones from all partitions

Get-ADDnsPartition | Get-ADDnsZone

Listing records

The following examples demonstrate how Get-DnsRecord can be used to pick up records configured on a server.

# Listing all records on the current server


# List A records in domain.example only

Get-DnsRecord -RecordType A -Zone domain.example

# List all static records on the server

Get-DnsRecord -Filter “TimeStamp=0”

# Name is a regular expression and can be used for simple or complex filters

Get-DnsRecord -Name ‘_tcp’ -RecordType SRV

Get-ADDnsRecord may also be to retrieve record information. By its nature Get-ADDnsRecord is limited to querying records stored in Active Directory-integrated zones.

# All records in the DomainDnsZones partition


# All Service (SRV) records in all partitions

Get-ADDnsPartition | Get-ADDnsRecord | Where-Object { $_.RecordType -eq “SRV” }

# Name supports wildcards (used to build an LDAP filter)

Get-ADDnsRecord -Name “_gc*”

# List all records in a specific zone

Get-ADDnsZone domain.example | Get-ADDnsRecord

Unlike Get-DnsRecord, which offers a parameter to filter on RecordType, Get-ADDnsRecord is reliant on Where-Object. This is used because
is no way to construct an LDAP Filter to limit a search to specific record types as the record type is encoded as part of the dnsRecord
attribute. Wildcards or partial matches are not permissible for DnsRecord as it is a Binary Large Object (BLOB).

The RecordType field used above is defined in an Enumeration. The possible values can be seen with:


Using the following filter for Where-Object returns the same results, it is equivalent to the filter above:

# Get all records from AD (DomainDnsZones). Filter to Service Records

Get-ADDnsRecord | Where-Object { $_.RecordType -eq [DnsShell.RecordType]::SRV }

Returning server configuration

Returning a DNS servers configuration is a simple task. The Get-DnsServer cmdlets returns each of the configuration options available to MS DNS server.

# Ask for DNS Server settings from $ServerName

Get-DnsServer $ServerName

Creating zones

New-DnsZone uses the CreateZone method from WMI, checking for a few potential errors along the way.

# A new standard Primary forward lookup zone. Returns an object representing the new zone

New-DnsZone domain.example -ZoneType Primary -PassThru

Creating records

New-DnsRecord is one of the most complex cmdlets in the module. It provides an abstract interface to the CreateInstanceFromPropertyData method on each of the record classes.
Many of the parameters accept pipeline input to further simplify usage.

# A new Host (A) record

New-DnsRecord -Name mail -RecordType A -Zone domain.example -IPAddress

# A new Mail Exchanger (MX) record

New-DnsRecord -RecordType MX -Zone domain.example ‘

-TargetName mail.domain.example -Preference 10

Advanced tasks

Automating secondary zone setup

On occasion it is nice to be able to create secondary zones for every Primary zone on another server.

# Alternate credentials for this operation

$Credential = Get-Credential

# The Primary server name (used to access WMI) and the  IP address $SecondaryServer will use to access the Primary

$PrimaryServer = “ThePrimaryServer”; $PrimaryIP = “”

# A secondary server name (used to access WMI)

$SecondaryServer = “TheSecondaryServer”

# Get all Primary Forward Lookup Zones from $PrimaryServer and create a corresponding Secondary zone on $SecondaryServer

Get-DnsZone -ZoneType Primary -Filter “Reverse=$False” ‘

-Server $PrimaryServer -Credential $Credential |

New-DnsZone -ZoneType Secondary -MasterServer $PrimaryIP  -Server $SecondaryServer -PassThru

A ForEach (either ForEach or ForEach-Object) loop is not necessary to accomplish this, however it may be added if greater control over the process is required.

Get-DnsZone -ZoneType Primary -Filter “Reverse=$False” -Server $PrimaryServer -Credential $Credential |

# See if the zone exists on $SecondaryServer first
If (!(Get-DnsZone $_.Name -Server $SecondaryServer)) {
# If it does not (!), create the zone
$_.Name -ZoneType Secondary -MasterServer $PrimaryIP -Server $SecondaryServer -PassThru

Strictly speaking, the ForEach-Object loop is still not an absolute requirement in this example. Where-Object can be used to filter down to zones that only do not exist on the Primary.

Adding A and PTR records from a CSV file

One common task is the addition of A and corresponding PTR records. To demonstrate this script a simple CSV file can be used.


The tricky part is adding the PTR records without having to manually define the Reverse Lookup Zone name. One possible way to deal with this is to send a
DNS query; an approach is similar to that used by dynamic update, a query which attempts to find a server willing to accept an update.

In the example below, Get-Dns is used to execute a query for the PTR record. The expected response is NXDOMAIN (doesn’t exist), but that response will contain an Authority section including the server and zone name.
That zone name can be used to make the new record.

$ServerName = “TheServer”
$ForwardLookupZone = “domain.example”
Import-Csv Records.csv | ForEach-Object {
# Create the record in the Forward Lookup Zone
New-DnsRecord -Name $_.Name -IPAddress $_.IPAddress -Zone $ForwardLookupZone -Type A -Server $ServerName
# Find the name of the reverse lookup zone
$ReverseLookupZone = (Get-Dns $_.IPAddress -RecordType PTR -Server $ServerName).Authority[0].Name
# Create the record in the Reverse Lookup Zone
New-DnsRecord -Name $_.IPAddress -Hostname “$($_.Name).$ForwardLookupZone” -Zone $ReverseLookupZone -Type PTR -Server $ServerName

Debugging name resolution with Get-Dns

Debugging name resolution under Microsoft Windows is typically limited to either nslookup or the client resolver (normally via ping). Get-Dns is more comprehensive, capable of both simple and complex queries.

Note: The examples below make extensive use of domain.example.
This should be replaced with a valid domain name, many of the examples will return nothing if taken literally.

The following command performs a Zone Transfer, used when a Secondary server picks up a copy of the zone from a Primary Server. This operation should not be confused with replication of zone data when Active Directory-integrated zones are in use. This command requires access to TCP Port 53 on the server holding the zone (most DNS traffic uses UDP Port 53).

# Equivalent of nslookup using ls -d domain.example
Get-Dns domain.example -Transfer
# Or
Get-Dns domain.example axfr

Tracing a request from root using an iterative query; used to verify the delegation chain, to test that DNS requests are directed to the correct servers. The initial servers (Root Hints) are taken from the locally configured DNS service.

# Performs a search for the record from the Root DNS servers (display all hops)
# Equivalent of dig domain.example +trace
Get-Dns domain.example -Iterative

NsSearch can be used to check that all DNS servers for a domain name reply with the same (or intended) answer. If the answers are different clients may experience problems accessing
a resource.

# Returns the A record for domain.example from all authoritative name servers
# Equivalent for dig domain.example a +nssearch
Get-Dns domain.example -RecordType A -NsSearch

Get-Dns returns all of the fields from a DNS packet by default; this makes the return value a complex nested object. Specific parts of the return value can be selected as demonstrated in the following examples.

# Expanding the answer
Get-Dns http://www.domain.example a -NsSearch | Select-Object -ExpandProperty Answer
# If only one response is returned
(Get-Dns http://www.domain.example a).Answer
# Just the Header
Get-Dns domain.example | Select-Object -ExpandProperty Header
# Question, Answer, Authority and Additional are always arrays. An element number must be used when accessing fields even if only one item exists.
(Get-Dns http://www.domain.example a).Question[0].Name
(Get-Dns http://www.domain.example a).Question.Count
(Get-Dns http://www.domain.example a).Question.GetType()
# Answer and Flags
Get-Dns domain.example | Select-Object Answer, @{n=’Flags’;e={ $_.Header.Flags

# The server, status code (RCode) and time taken (in milliseconds) for an Iterative query
Get-Dns domain.exmaple -Iterative | Select-Object Server, TimeTaken,
@{n=’RCode’;e={ $_.Header.RCode }}

Published by jdalbera

IT Pro: 25 years experience for large companies - Technical manager and solution architect: Directory services and Identity Management, Azure AD, Office 365, Azure infrastructures, Microsoft AD Security (ADDS,ADFS,ADCS), PowerShell, Quest solutions architect. Operating systems (Win/Lin). Unix and Microsoft interoperability. Data center Operations. Company integrations. Network architectures. Virtualization and storage infrastructures. HP/Dell servers deployments. Certifications: MCSE, MCPs, MCITS, ITIL, VCP, CCNA, CyberArk

%d bloggers like this: