Skip to content

Instantly share code, notes, and snippets.

@machv
Created April 8, 2019 07:48
Show Gist options
  • Save machv/e9b33bb59068349d4913e2924151a3e2 to your computer and use it in GitHub Desktop.
Save machv/e9b33bb59068349d4913e2924151a3e2 to your computer and use it in GitHub Desktop.
Periodic DNS resolving test
function Test-DnsResolving($DnsServer, $Delay, $Query)
{
begin {
$testsCount = 0
$latencySum = 0
$max = [Double]0
$min = [Double]99999
[Console]::TreatControlCAsInput = $true
}
process {
while($true) {
if([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true)
if ($key.Key -eq "C" -and $key.Modifiers -eq "Control") {
break
}
}
$resolverResponse = $null
$time = (Measure-Command { $resolverResponse = Resolve-DnsName $Query -Server $DnsServer -DnsOnly }).Milliseconds
$records = $resolverResponse.Count
if($time -gt $max)
{
$max = $time
}
if($time -lt $min)
{
$min = $time
}
$latencySum += $time
$testsCount += 1
("{0}: {1} ms ({2} records returned)" -f (Get-Date), $time, $records)
Start-Sleep -Seconds $Delay
}
}
end {
[Console]::TreatControlCAsInput = $true
""
"{0} tests executed" -f $testsCount
"Minimum = {0}ms, Maximum = {1}ms, Average = {2}ms" -f $min, $max, [Math]::Round(([Double]$latencySum / $testsCount))
""
}
}
# Get connected adapter
$adapter = Get-NetAdapter | Where-Object Status -EQ Up | Select -First 1
# Get adapter's DNS servers
$servers = (Get-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex -AddressFamily IPv4) | Select-Object -ExpandProperty ServerAddresses
# Use first DNS server and run test (test can be cancelled by CTRL-C)
Test-DnsResolving -DnsServer $servers[0] -Delay 3 -Query "outlook.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment