56 lines
2.0 KiB
PowerShell
Executable File
56 lines
2.0 KiB
PowerShell
Executable File
#Requires -RunAsAdministrator
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
Configures static DNS server addresses on active network adapters.
|
|
|
|
.DESCRIPTION
|
|
This script retrieves all active (connected) network interfaces on the endpoint and assigns
|
|
primary DNS (Quad9) and secondary backup DNS (Cloudflare) servers.
|
|
|
|
.NOTES
|
|
Created: 2025-03-02
|
|
Modified: 2026-10-04
|
|
|
|
DNS Server Mappings:
|
|
- Quad9 Primary: 9.9.9.9 (Malware blocking + DNSSEC)
|
|
- Quad9 Secondary: 149.112.112.11 (Malware blocking + DNSSEC)
|
|
- Cloudflare Prim: 1.1.1.2 (Malware blocking)
|
|
- Cloudflare Sec: 1.0.0.2 (Malware blocking)
|
|
|
|
* Note: If adult content blocking is also desired via Cloudflare,
|
|
use 1.1.1.3 and 1.0.0.3 instead.
|
|
#>
|
|
|
|
[CmdletBinding(SupportsShouldProcess = $true)]
|
|
param (
|
|
# Array of DNS IPv4 addresses in priority order
|
|
[string[]]$DnsServers = @("9.9.9.9", "149.112.112.11", "1.1.1.2", "1.0.0.2")
|
|
)
|
|
|
|
# 1. Retrieve active/connected network adapters (filters out disabled, virtual, or disconnected NICs)
|
|
$ActiveAdapters = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }
|
|
|
|
if (-not $ActiveAdapters) {
|
|
Write-Warning "No active network adapters found on this endpoint."
|
|
return
|
|
}
|
|
|
|
# 2. Apply DNS settings to each active interface
|
|
foreach ($Adapter in $ActiveAdapters) {
|
|
|
|
$InterfaceName = $Adapter.Name
|
|
$InterfaceIndex = $Adapter.InterfaceIndex
|
|
|
|
if ($PSCmdlet.ShouldProcess("Interface: $InterfaceName (Index: $InterfaceIndex)", "Set DNS servers to $($DnsServers -join ', ')")) {
|
|
try {
|
|
# Set the static DNS client addresses
|
|
Set-DnsClientServerAddress -InterfaceIndex $InterfaceIndex -ServerAddresses $DnsServers -ErrorAction Stop
|
|
|
|
Write-Host "Successfully updated DNS for '$InterfaceName' (Index: $InterfaceIndex)" -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Error "Failed to update DNS on interface '$InterfaceName' (Index: $InterfaceIndex). Error: $_"
|
|
}
|
|
}
|
|
} |