Advertisement
Chibibowa

Set DNS on all network adapters

Apr 22nd, 2024
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.48 KB | Source Code | 0 0
  1. # PowerShell Script to Set DNS Servers on All Network Adapters and Validate Them
  2. # Save this file as SetDNS.ps1
  3.  
  4. # Ensure the script runs as Administrator
  5. if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  6.     Write-Host "`nAttempting to relaunch with administrative privileges..." -ForegroundColor Yellow
  7.     Start-Sleep -Seconds 2
  8.     Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
  9.     exit
  10. }
  11.  
  12. # Set execution policy temporarily to unrestricted
  13. Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force
  14.  
  15. # Display script information and ask for user confirmation to proceed
  16. Write-Host "`nThis script will change the DNS settings based on the Ultimate Hosts Blacklist to protect against malicious actors." -ForegroundColor Cyan
  17. Write-Host "For more details, visit the GitHub page: https://github.com/Ultimate-Hosts-Blacklist/Ultimate.Hosts.Blacklist" -ForegroundColor Cyan
  18. Start-Sleep -Seconds 2
  19.  
  20. Write-Host "`nDNS servers to be set:"
  21. Write-Host "IPv4 Primary:   88.198.70.38" -ForegroundColor Yellow
  22. Write-Host "IPv4 Secondary: 88.198.70.39" -ForegroundColor Yellow
  23. Write-Host "IPv6 Primary:   2a01:4f8:140:5021::38" -ForegroundColor Yellow
  24. Write-Host "IPv6 Secondary: 2a01:4f8:140:5021::39`n" -ForegroundColor Yellow
  25.  
  26. $confirmation = Read-Host "Do you want to proceed with setting these DNS servers? (Y/N)"
  27. if ($confirmation -ne 'Y') {
  28.     Write-Host "`nOperation cancelled by user. No changes made.`n" -ForegroundColor Red
  29.     exit
  30. }
  31.  
  32. # DNS server addresses
  33. $IPv4PrimaryDNS = "88.198.70.38"
  34. $IPv4SecondaryDNS = "88.198.70.39"
  35. $IPv6PrimaryDNS = "2a01:4f8:140:5021::38"
  36. $IPv6SecondaryDNS = "2a01:4f8:140:5021::39"
  37.  
  38. # Apply DNS settings to all network adapters
  39. Write-Host "`nApplying new DNS values to all network adapters..." -ForegroundColor Green
  40. Get-NetAdapter | ForEach-Object {
  41.     Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses $IPv4PrimaryDNS, $IPv4SecondaryDNS, $IPv6PrimaryDNS, $IPv6SecondaryDNS
  42. }
  43. Start-Sleep -Seconds 2
  44.  
  45. # Flush DNS to ensure new settings are used immediately
  46. Write-Host "`nFlushing DNS cache to ensure new settings are active..." -ForegroundColor Green
  47. ipconfig /flushdns
  48. Start-Sleep -Seconds 2
  49.  
  50. # Validate the DNS settings applied
  51. Write-Host "`nValidating the applied DNS settings:" -ForegroundColor Green
  52. $adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
  53. foreach ($adapter in $adapters) {
  54.     $dnsSettingsIPv4 = (Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4).ServerAddresses
  55.     $dnsSettingsIPv6 = (Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv6).ServerAddresses
  56.     Write-Host "Adapter: $($adapter.Name)"
  57.     Write-Host "IPv4 DNS: $($dnsSettingsIPv4 -join ', ')"
  58.     Write-Host "IPv6 DNS: $($dnsSettingsIPv6 -join ', ')"
  59. }
  60. Start-Sleep -Seconds 2
  61.  
  62. # Confirm the DNS settings applied
  63. Write-Host "`nDNS settings have been successfully set and verified on active adapters:" -ForegroundColor Green
  64. Write-Host "IPv4 Primary DNS: $IPv4PrimaryDNS"
  65. Write-Host "IPv4 Secondary DNS: $IPv4SecondaryDNS"
  66. Write-Host "IPv6 Primary DNS: $IPv6PrimaryDNS"
  67. Write-Host "IPv6 Secondary DNS: $IPv6SecondaryDNS"
  68.  
  69. # Wait for user to read the output before exiting
  70. Write-Host "`nPress any key to exit..." -ForegroundColor White
  71. $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement