-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork-FixDns.ps1
More file actions
64 lines (55 loc) · 2.41 KB
/
Network-FixDns.ps1
File metadata and controls
64 lines (55 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env pwsh
# Simple DNS Cleanup Tool
# Fixes broken DNS configurations safely
Write-Host "=== DNS Cleanup Tool ===" -ForegroundColor Cyan
Write-Host "Checking for broken DNS servers..." -ForegroundColor Gray
$brokenDns = @()
$allDns = Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses.Count -gt 0 }
foreach ($dnsConfig in $allDns) {
$interfaceStatus = (Get-NetAdapter -Name $dnsConfig.InterfaceAlias -ErrorAction SilentlyContinue).Status
Write-Host "Testing $($dnsConfig.InterfaceAlias) ($interfaceStatus)..." -ForegroundColor Gray
foreach ($server in $dnsConfig.ServerAddresses) {
Write-Host " Checking $server..." -NoNewline
try {
$null = Resolve-DnsName -Name "google.com" -Server $server -ErrorAction Stop -TimeoutInSeconds 3
Write-Host " OK" -ForegroundColor Green
} catch {
Write-Host " BROKEN" -ForegroundColor Red
$brokenDns += @{
Interface = $dnsConfig.InterfaceAlias
Status = $interfaceStatus
Server = $server
}
}
}
}
if ($brokenDns.Count -eq 0) {
Write-Host "`nAll DNS servers are working! No cleanup needed." -ForegroundColor Green
exit 0
}
Write-Host "`nFound broken DNS servers:" -ForegroundColor Yellow
foreach ($broken in $brokenDns) {
Write-Host " $($broken.Server) on $($broken.Interface) ($($broken.Status))" -ForegroundColor Red
}
$fix = Read-Host "`nFix these automatically? (y/N)"
if ($fix -notmatch '^[Yy]') {
Write-Host "No changes made." -ForegroundColor Gray
exit 0
}
Write-Host "`nFixing DNS issues..." -ForegroundColor Cyan
foreach ($broken in $brokenDns) {
Write-Host "Fixing $($broken.Interface)..." -ForegroundColor Yellow
try {
if ($broken.Status -eq "Up") {
Set-DnsClientServerAddress -InterfaceAlias $broken.Interface -ServerAddresses @("8.8.8.8", "8.8.4.4")
Write-Host " Set reliable DNS" -ForegroundColor Green
} else {
Set-DnsClientServerAddress -InterfaceAlias $broken.Interface -ResetServerAddresses
Write-Host " Cleared DNS from disconnected interface" -ForegroundColor Green
}
} catch {
Write-Host " Failed: $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host "`nDNS cleanup complete!" -ForegroundColor Green
Write-Host "Test with: .\Network-DetailedDiagnostics.ps1" -ForegroundColor Cyan