Usage: .\mwDNSTester.ps1 [-h]

This script performs periodic DNS queries to specified servers for a list of domain names.

Parameters:
-h Display this help message.

The script will run indefinitely, querying the DNS servers every 30 seconds by default.
You can modify the DNS names, servers, and interval within the script.

# Note on Logging Output:
# To log the output while still displaying it on the console, you can pipe the output to Tee-Object, like this:
#
# .\mwDNSTester.ps1 | Tee-Object -FilePath "dns_log.txt" -Append
#
# This will display the output on the screen and also append it to 'dns_log.txt'.


===================================================================================================
here comes the script: mwDNSTester.ps1
===================================================================================================

param (
[switch]$h
)

# Define DNS names to query
$dnsNames = @(
"example.com",
"google.com",
"microsoft.com",
"computer2know.de",
)

# Define DNS servers to query
$dnsServers = @(
"1.1.1.1",
"8.8.8.8"
)


# Set the interval in seconds
$interval = 30


if ($h) {
Write-Output @"
Usage: .\mwDNSTester.ps1 [-h]

This script performs periodic DNS queries to specified servers for a list of domain names.

Parameters:
-h Display this help message.

The script will run indefinitely, querying the DNS servers every 30 seconds by default.
You can modify the DNS names, servers, and interval within the script.

# Note on Logging Output:
# To log the output while still displaying it on the console, you can pipe the output to Tee-Object, like this:
#
# .\mwDNSTester.ps1 | Tee-Object -FilePath "dns_log.txt" -Append
#
# This will display the output on the screen and also append it to 'dns_log.txt'.
"@
exit
}

# Function to perform DNS query using Resolve-DnsName with specific server
function Test-DnsServer {
param (
[string]$dnsName,
[string]$dnsServer
)

$startTime = Get-Date
$status = "FAILED"
$response = ""
try {
# Perform the DNS query
$result = Resolve-DnsName -Name $dnsName -Server $dnsServer -ErrorAction Stop

$queryTime = (Get-Date) - $startTime

# Extract the first answer's hostname or IP
if ($result) {
# Try to get the first answer's NameHost or IPAddress
$firstRecord = $result | Select-Object -First 1
if ($firstRecord) {
if ($firstRecord.PSObject.Properties.Name -contains "NameHost") {
$response = $firstRecord.NameHost
} elseif ($firstRecord.PSObject.Properties.Name -contains "IPAddress") {
$response = $firstRecord.IPAddress
} else {
$response = "No response"
}
$status = "OK"
} else {
$response = "No response"
}
} else {
$response = "No response"
}
} catch {
$queryTime = (Get-Date) - $startTime
$response = "Error: $_"
$status = "FAILED"
}

# Format and return the output
"{0};{1};{2};{3};{4};{5}" -f `
(Get-Date -Format "yyyy-MM-dd HH:mm:ss"), `
$dnsServer, `
$dnsName, `
$response, `
("{0:N2} ms" -f $queryTime.TotalMilliseconds), `
$status
}

# Main loop
while ($true) {
foreach ($dnsServer in $dnsServers) {
foreach ($dnsName in $dnsNames) {
$output = Test-DnsServer -dnsName $dnsName -dnsServer $dnsServer
Write-Output $output
}
}
Start-Sleep -Seconds $interval
}

computer2know :: thank you for your visit :: have a nice day :: © 2025