Pester script to Test DNS Configuration

So I recently blogged about Configuring DNS zones and resource records. While going through my twitter feed, I stumbled upon this little gem by Kevin Marquette. He recently did a session on Pester and uploaded his demo. If you’re interested in Pester (as you should) you should definitely check it out!

So one of the demos was a eureka moment for me: The Active.Directory.System.DC.tests.ps1!

Wait you can do that? I thought Pester was about unit Framework testing not about validating script output. So I can test if my script did what I expected it to do? (Pause to let that sink in). Well alrighty then!!! 😛

So I decided to give it a go for the DNS Configuration.


<#
Author: I.C.A. Strachan
Version:
Version History:
Purpose: Pester script to validate that DNS Zones and Records have been configured
#>
[CmdletBinding()]
Param(
[string]$fqdn ='domain.local',
[string[]]$ServerIPAddress= @('192.168.1.4', '192.168.1.5')
)
Import-Module DNSServer -Verbose:$false
Describe "DNS Exchange Configuration Test for $fqdn" {
$zoneNames = @(
"autodiscover.$($fqdn)"
"mail.$($fqdn)"
"webmail.$($fqdn)"
"_autodiscover._tcp.$($fqdn)"
)
Context 'DNS Zones' {
# Test every zone
foreach($zoneName in $zoneNames){
it "Has a zone named: $zoneName" {
Get-DnsServerZone $zoneName | Should Not BeNullOrEmpty
}
}
}
Context 'DNS Resource records' {
foreach ($IPAddress in $ServerIPAddress){
foreach($zoneName in $zoneNames){
It "Has IPAddress $($IPAddress) in zone $($zoneName)"{
(Get-DnsServerResourceRecord -ZoneName $zoneName | out-string) | Should Match $IPAddress
}
}
}
}
}

And here’s a screenshot of the results:

Pester-DNS

So there’s more to Pester that meets the eye… Imagine the possibilities… No wonder Microsoft is shipping Pester with 2016…

Hope it’s worth something to you

Ttyl,

Urv

3 thoughts on “Pester script to Test DNS Configuration

  1. Pingback: Pester as an Operation Validation Framework | Programming, PowerShell and Pants

  2. Pingback: Continuously Testing your Infrastructure with OVF and Microsoft Operations Management Suite | PowerShell, Programming and DevOps

  3. ofirdoron

    Irwin my friend!
    I reviewed this script too… it should be something like this:
    (srv records don’t use IP addresses…)

    [CmdletBinding()]
    Param(
    [Parameter( Mandatory=$true,
    ValueFromPipeline=$false)]
    [string]$fqdn =’contoso.com’,
    [Parameter( Mandatory=$true,
    ValueFromPipeline=$false)]
    [string[]]$IPAddresses= @(‘192.168.1.11’, ‘192.168.1.12’)
    )

    Import-Module DNSServer -Verbose:$false

    Describe “DNS Exchange Configuration Test for $fqdn” {

    $zoneNames = @(
    “autodiscover.$($fqdn)”
    “mail.$($fqdn)”
    “_autodiscover._tcp.$($fqdn)”
    )

    Context ‘DNS Zones’ {

    # Test every zone
    foreach($zoneName in $zoneNames){
    it “Has a zone named: $zoneName” {

    Get-DnsServerZone $zoneName | Should Not BeNullOrEmpty
    }
    }
    }

    Context ‘DNS Resource records’ {

    foreach ($IPAddress in $IPAddresses){

    foreach($zoneName in $zoneNames){

    If ($zoneName -eq “_autodiscover._tcp.$($fqdn)”) {Continue;}

    It “Has IPAddress $($IPAddress) in zone $($zoneName)”{
    (Get-DnsServerResourceRecord -ZoneName $zoneName | out-string) | Should Match $IPAddress
    }
    }
    }
    }

    Context ‘DNS SRV records’ {

    foreach($zoneName in $zoneNames){

    If ($zoneName -ne “_autodiscover._tcp.$($fqdn)”) {Continue;}

    It “Has SRV DNS Record $($zoneName) in zone $($fqdn)”{
    (Get-DnsServerResourceRecord -ZoneName $zoneName -RRType Srv | out-string) | Should Match “autodiscover.$($fqdn)”

    }
    }
    }
    }

    Like

    Reply

Leave a comment