Public/Diagnostics/Test-DNSResolution.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Test-DNSResolution {
    <#
    .SYNOPSIS
    Tests DNS resolution for specified domain names.
 
    .DESCRIPTION
    This function tests the DNS resolution for a list of domain names multiple times and categorizes the results based on failure thresholds.
 
    .PARAMETER CriticalThreshold
    The number of failed attempts to categorize the result as critical. Default is 3.
 
    .PARAMETER ErrorThreshold
    The number of failed attempts to categorize the result as an error. Default is 2.
 
    .PARAMETER WarningThreshold
    The number of failed attempts to categorize the result as a warning. Default is 1.
 
    .PARAMETER TestCount
    The number of times to test each domain name. Default is 5.
 
    .PARAMETER Name
    The list of domain names to test.
 
    .PARAMETER IncludeRandom
    Switch to include a predefined list of common domain names if no names are provided.
 
    .EXAMPLE
    Test-DNSResolution -Name "example.com" -TestCount 3
 
    .NOTES
    The function uses Resolve-DNSName to test DNS resolution and categorizes the results based on the specified thresholds.
    #>


    [CmdletBinding()]
    PARAM (
        [int]$CriticalThreshold = 3,
        [int]$ErrorThreshold = 2,
        [int]$WarningThreshold = 1,
        [int]$TestCount = 4,
        [string[]]$Name = @(), 
        [switch]$IncludeRandom = $False
    )

    IF ($IncludeRandom -eq $True -or $Name.Count -eq 0) {
        $Name += "Google.com"
        $Name += "Microsoft.com"
        $Name += "IntegrisIT.com"
        $Name += "BlueJeanNetworks.com"
        $Name += "IconicIT.com"
        $Name += "MyITPros.com"
        $Name += "Salesforce.com"
        $Name += "Whitehouse.gov"
        $Name += "Namecheap.com"
        $Name += "sourceforge.net"
        $Name += "cloudfront.net"
        $Name += "windows.net"
        $Name += "mozilla.org"
        $Name += "worldwildlife.org"
        $Name += "theheartfoundation.org"
        $Name += "mayoclinic.org"
    }

    $Results = @()
    $TotalTestCount = $TestCount * $Name.Length
    $Count = 0
    $FailCount = 0
    $CurrentCount = 0
    
    FOREACH ($Item in $Name) {
        IF ($Item -like "*reddog*") { continue }
        $Count = 0
        $FailCount = 0
        FOR ($i = 0; $i -lt $TestCount; $i++) {
            $Count++
            $CurrentCount++
            $ProgressPercent = [Math]::Round((($CurrentCount-1) / $TotalTestCount) * 100,0)
            Write-Progress -ID 8 -Activity "Testing DNS" -Status "$ProgressPercent% - Testing Resolution of $Item" -PercentComplete $ProgressPercent
            $DNSResult = $null
            $DNSResult = Resolve-DNSName -Name $Item -Type A -QuickTimeout -ErrorAction SilentlyContinue
            IF ($null -eq $DNSResult) { $FailCount++; IF ($FailCount -ge $CriticalThreshold) { break }}
            Start-Sleep -Milliseconds 50
        }

        IF ($FailCount -ge $CriticalThreshold) { $Verdict = "Critical" }
        ELSEIF ($FailCount -ge $ErrorThreshold) { $Verdict = "Error" }
        ELSEIF ($FailCount -ge $WarningThreshold) { $Verdict = "Warning" }
        ELSE { $Verdict = "Normal" }

        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            PSTypeName = 'IntegrisPowerShell.DNSResolution'
            Name = $Item
            TestCount = $Count
            FailCount = $FailCount
            FailPercentage = $FailCount / $Count * 100
            Verdict = $Verdict
        }
    }

    Write-Progress -ID 8 -Activity "Checking External DNS" -Status "99% - Preparing Results" -PercentComplete 100 -Completed

    RETURN $Results
}
New-Alias -Name Test-DNS -Value Test-DNSResolution