Back to Resources
Level
Script
General
Unexpected or incorrect DNS settings can reroute traffic to unauthorized servers, slow down network performance, or disrupt critical services. Ensuring that endpoints are always using approved DNS servers mitigates security risks and supports reliable connectivity.
This script retrieves the DNS settings from all network adapters on a Windows system and compares them against a specified list of authorized DNS servers, managed via the cf_dns
Level custom field. If the DNS server configuration fully aligns with the allowed list, the script indicates success. Otherwise, it returns an alert, letting you know immediately if a system has strayed from standard policies.
You can run this script on demand by configuring a script-based monitor in Level, triggering checks whenever you suspect a configuration change. Alternatively, you can schedule it through a Level Automation for ongoing compliance checks, automatically alerting you when any system falls out of spec.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-monitor-dns-servers
#>
# Comma-separated list of expected DNS servers
$allowedDnsServers = "{{cf_dns}}"
# Convert the comma-separated string into an array
$allowedDnsServersArray = $allowedDnsServers -split "\s*,\s*"
# -----------------------------------------------------------------------------
# Function to check if the DNS servers match the allowed list
function Check-DnsServers {
$networkInterfaces = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled }
foreach ($interface in $networkInterfaces) {
$dnsServers = $interface.DNSServerSearchOrder
Write-Host "Interface: $($interface.Description)"
Write-Host "Allowed DNS servers: $($allowedDnsServersArray -join ', ')"
Write-Host "Current DNS servers: $($dnsServers -join ', ')"
if ($dnsServers -ne $null -and $dnsServers.Count -gt 0) {
$matchingServers = @($dnsServers | Where-Object { $allowedDnsServersArray -contains $_ })
if ($matchingServers.Count -eq $dnsServers.Count) {
Write-Host "SUCCESS: DNS servers match the allowed list."
exit 0
} else {
Write-Host "ALERT: Not all DNS servers are in the allowed list."
exit 1
}
} else {
Write-Host "ALERT: No DNS servers configured"
exit 0
}
}
}
# Check if the DNS servers match the allowed list
Check-DnsServers
cf_dns
custom field to reflect current authorized serverscf_dns
field in Level with a comma-separated list of valid DNS addresses.Windows Monitor - DNS Servers
This script verifies that the DNS servers configured on all active network interfaces match a predefined list of allowed servers. It compares the current DNS servers against the allowed list and outputs "SUCCESS" if they all match or "ALERT" if any discrepancies are found or no DNS servers are configured.
PowerShell
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.