-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork-DnsCleanup.ps1
More file actions
92 lines (79 loc) · 3.71 KB
/
Network-DnsCleanup.ps1
File metadata and controls
92 lines (79 loc) · 3.71 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Network DNS Configuration Cleanup Tool
# Safely detects and fixes broken DNS server configurations
# Author: AI Assistant
# Date: 2025-10-10
Write-Host "=== DNS Configuration Cleanup Tool ===" -ForegroundColor Cyan
Write-Host "This tool will safely check and fix DNS configuration issues." -ForegroundColor Gray
Write-Host ""
# Check for broken DNS servers
Write-Host "Analyzing DNS configuration..." -ForegroundColor Yellow
$problemDns = @()
$dnsConfigs = Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses.Count -gt 0 }
foreach ($config in $dnsConfigs) {
$interfaceStatus = (Get-NetAdapter -Name $config.InterfaceAlias -ErrorAction SilentlyContinue).Status
Write-Host "Checking $($config.InterfaceAlias) (Status: $interfaceStatus)" -ForegroundColor Gray
foreach ($dns in $config.ServerAddresses) {
try {
Write-Host " Testing DNS $dns..." -ForegroundColor Gray -NoNewline
$null = Resolve-DnsName -Name "google.com" -Server $dns -Type A -ErrorAction Stop -TimeoutInSeconds 3
Write-Host " WORKING" -ForegroundColor Green
} catch {
Write-Host " FAILED" -ForegroundColor Red
$problemDns += @{
Interface = $config.InterfaceAlias
Status = $interfaceStatus
DNS = $dns
}
}
}
}
if ($problemDns.Count -eq 0) {
Write-Host "`n✓ All DNS servers are working properly!" -ForegroundColor Green
Write-Host "No cleanup needed." -ForegroundColor White
exit 0
}
# Show problems and ask for permission to fix
Write-Host "`n⚠ Found $($problemDns.Count) broken DNS configuration(s):" -ForegroundColor Yellow
foreach ($problem in $problemDns) {
Write-Host " • $($problem.DNS) on $($problem.Interface) ($($problem.Status))" -ForegroundColor Red
}
Write-Host "`nWould you like to fix these automatically? This will:" -ForegroundColor Cyan
Write-Host " • Clear broken DNS from disconnected interfaces" -ForegroundColor White
Write-Host " • Replace broken DNS on active interfaces with Google DNS (8.8.8.8)" -ForegroundColor White
Write-Host ""
$consent = Read-Host "Proceed with cleanup? (y/N)"
if ($consent -notmatch '^[Yy]') {
Write-Host "Cleanup cancelled. No changes made." -ForegroundColor Gray
exit 0
}
# Perform safe cleanup
Write-Host "`nPerforming DNS cleanup..." -ForegroundColor Cyan
$fixed = 0
foreach ($problem in $problemDns) {
Write-Host "Fixing $($problem.Interface)..." -ForegroundColor Yellow
if ($problem.Status -eq "Up") {
# Active interface - replace with reliable DNS
try {
Set-DnsClientServerAddress -InterfaceAlias $problem.Interface -ServerAddresses @("8.8.8.8", "8.8.4.4")
Write-Host " ✓ Set reliable DNS on $($problem.Interface)" -ForegroundColor Green
$fixed++
} catch {
Write-Host " ✗ Failed to fix $($problem.Interface): $($_.Exception.Message)" -ForegroundColor Red
}
} else {
# Disconnected interface - clear DNS
try {
Set-DnsClientServerAddress -InterfaceAlias $problem.Interface -ResetServerAddresses
Write-Host " ✓ Cleared DNS from disconnected $($problem.Interface)" -ForegroundColor Green
$fixed++
} catch {
Write-Host " ✗ Failed to clear $($problem.Interface): $($_.Exception.Message)" -ForegroundColor Red
}
}
}
Write-Host "`n🎯 Cleanup complete! Fixed $fixed of $($problemDns.Count) issues." -ForegroundColor Green
Write-Host "Run Network-DetailedDiagnostics.ps1 again to verify the fixes." -ForegroundColor Cyan
if ($Host.Name -eq "ConsoleHost") {
Write-Host "`nPress Enter to close..." -ForegroundColor Gray
Read-Host
}