|
| 1 | +# Measure the number of network hops and the average response time for a query to a DNS server. |
| 2 | + |
| 3 | +function Measure-DnsResponseTime { |
| 4 | + <# |
| 5 | + .SYNOPSIS |
| 6 | + Measure average query response time from a DNS server. |
| 7 | +
|
| 8 | + .PARAMETER DnsServer |
| 9 | + The DNS server name or IP address to query. |
| 10 | +
|
| 11 | + .PARAMETER TargetName |
| 12 | + The domain or host name to query. |
| 13 | + #> |
| 14 | + [CmdletBinding()] |
| 15 | + [OutputType([double])] |
| 16 | + param ( |
| 17 | + [Parameter( |
| 18 | + Mandatory, |
| 19 | + Position = 0, |
| 20 | + HelpMessage = 'The DNS server name or IP address to query.')] |
| 21 | + [ValidateNotNullOrEmpty()] |
| 22 | + [string] |
| 23 | + $DnsServer, |
| 24 | + |
| 25 | + [Parameter( |
| 26 | + Mandatory, |
| 27 | + Position = 1, |
| 28 | + HelpMessage = 'The domain or host name to resolve.')] |
| 29 | + [ValidateNotNullOrEmpty()] |
| 30 | + [string] |
| 31 | + $TargetName |
| 32 | + ) |
| 33 | + $queryTimes = @() |
| 34 | + Write-Host "Querying DNS server $DnsServer for $TargetName 100 times: " -NoNewline -ForegroundColor Green |
| 35 | + for ($i = 0; $i -lt 100; $i++) { |
| 36 | + Write-Host '.' -NoNewline -ForegroundColor Yellow |
| 37 | + try { |
| 38 | + Clear-DnsClientCache |
| 39 | + $QueryTimes += (Measure-Command { [System.Net.Dns]::GetHostAddresses($TargetName) }).TotalMilliseconds |
| 40 | + # $QueryTimes += (Measure-Command { Resolve-DnsName -Server $DnsServer -Name $Targetname -DnsOnly -NoHostsFile }).TotalMilliseconds |
| 41 | + # $ResponseTime = (Get-History -Count 1).Duration.TotalMilliseconds |
| 42 | + } catch { |
| 43 | + Write-Output "Failed to resolve DNS query: $_" |
| 44 | + return |
| 45 | + # To Do: Add error handling. Change return to a continue and track how many times it failed, then reduce the result count for the average--but also show a factor for how reliable the server was. |
| 46 | + } |
| 47 | + } |
| 48 | + Write-Host ". Done!`n" -ForegroundColor Green |
| 49 | + "Times: $($QueryTimes -join ', ')" | Write-Verbose |
| 50 | + $AverageTime = [math]::Round( ($QueryTimes | Measure-Object -Average).Average, 2 ) |
| 51 | + Write-Host "Average response time: $AverageTime ms" -ForegroundColor Green |
| 52 | + $AverageTime |
| 53 | +} |
| 54 | + |
| 55 | +function Measure-NetworkHops { |
| 56 | + <# |
| 57 | + .SYNOPSIS |
| 58 | + Measure the number of network hops and get basic traceroute details for a given server. |
| 59 | +
|
| 60 | + .PARAMETER Server |
| 61 | + The server name or IP address to measure network hops to. |
| 62 | + #> |
| 63 | + [CmdletBinding()] |
| 64 | + [OutputType([int])] |
| 65 | + param ( |
| 66 | + [Parameter( |
| 67 | + Mandatory, |
| 68 | + Position = 0, |
| 69 | + HelpMessage = 'The DNS server name or IP address to measure network hops to.')] |
| 70 | + [ValidateNotNullOrEmpty()] |
| 71 | + [string] |
| 72 | + $Server |
| 73 | + ) |
| 74 | + Write-Host "Measuring network hops to $Server..." -ForegroundColor Yellow |
| 75 | + $TestResult = Test-NetConnection -ComputerName $Server -TraceRoute -InformationLevel Detailed |
| 76 | + $Result = [PSCustomObject]@{ |
| 77 | + Server = $Server |
| 78 | + PingSucceeded = $TestResult.PingSucceeded |
| 79 | + PingRoundTripTime = $TestResult.PingReplyDetails.RoundtripTime |
| 80 | + Hops = $TestResult.TraceRoute.Count |
| 81 | + # NameResolutionSucceeded = $TestResult.NameResolutionSucceeded |
| 82 | + # ResolvedName = $TestResult.DNSOnlyRecords.Name |
| 83 | + MatchingIpsecRules = $TestResult.MatchingIpsecRules |
| 84 | + } |
| 85 | + $Result |
| 86 | +} |
| 87 | + |
| 88 | +# Example Usage: |
| 89 | +$DnsServers = @('8.8.8.8', '8.8.4.4', '1.1.1.1', '9.9.9.9') |
| 90 | +$DnsServers | ForEach-Object { |
| 91 | + Measure-NetworkHops -Server $_ |
| 92 | + Measure-DnsResponseTime -DnsServer $_ -TargetName 'day3bits.com' | Out-Null |
| 93 | +} |
0 commit comments