Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PowerShell Script to Set DNS Servers on All Network Adapters and Validate Them
- # Save this file as SetDNS.ps1
- # Ensure the script runs as Administrator
- if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- Write-Host "`nAttempting to relaunch with administrative privileges..." -ForegroundColor Yellow
- Start-Sleep -Seconds 2
- Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
- exit
- }
- # Set execution policy temporarily to unrestricted
- Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force
- # Display script information and ask for user confirmation to proceed
- Write-Host "`nThis script will change the DNS settings based on the Ultimate Hosts Blacklist to protect against malicious actors." -ForegroundColor Cyan
- Write-Host "For more details, visit the GitHub page: https://github.com/Ultimate-Hosts-Blacklist/Ultimate.Hosts.Blacklist" -ForegroundColor Cyan
- Start-Sleep -Seconds 2
- Write-Host "`nDNS servers to be set:"
- Write-Host "IPv4 Primary: 88.198.70.38" -ForegroundColor Yellow
- Write-Host "IPv4 Secondary: 88.198.70.39" -ForegroundColor Yellow
- Write-Host "IPv6 Primary: 2a01:4f8:140:5021::38" -ForegroundColor Yellow
- Write-Host "IPv6 Secondary: 2a01:4f8:140:5021::39`n" -ForegroundColor Yellow
- $confirmation = Read-Host "Do you want to proceed with setting these DNS servers? (Y/N)"
- if ($confirmation -ne 'Y') {
- Write-Host "`nOperation cancelled by user. No changes made.`n" -ForegroundColor Red
- exit
- }
- # DNS server addresses
- $IPv4PrimaryDNS = "88.198.70.38"
- $IPv4SecondaryDNS = "88.198.70.39"
- $IPv6PrimaryDNS = "2a01:4f8:140:5021::38"
- $IPv6SecondaryDNS = "2a01:4f8:140:5021::39"
- # Apply DNS settings to all network adapters
- Write-Host "`nApplying new DNS values to all network adapters..." -ForegroundColor Green
- Get-NetAdapter | ForEach-Object {
- Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses $IPv4PrimaryDNS, $IPv4SecondaryDNS, $IPv6PrimaryDNS, $IPv6SecondaryDNS
- }
- Start-Sleep -Seconds 2
- # Flush DNS to ensure new settings are used immediately
- Write-Host "`nFlushing DNS cache to ensure new settings are active..." -ForegroundColor Green
- ipconfig /flushdns
- Start-Sleep -Seconds 2
- # Validate the DNS settings applied
- Write-Host "`nValidating the applied DNS settings:" -ForegroundColor Green
- $adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
- foreach ($adapter in $adapters) {
- $dnsSettingsIPv4 = (Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4).ServerAddresses
- $dnsSettingsIPv6 = (Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv6).ServerAddresses
- Write-Host "Adapter: $($adapter.Name)"
- Write-Host "IPv4 DNS: $($dnsSettingsIPv4 -join ', ')"
- Write-Host "IPv6 DNS: $($dnsSettingsIPv6 -join ', ')"
- }
- Start-Sleep -Seconds 2
- # Confirm the DNS settings applied
- Write-Host "`nDNS settings have been successfully set and verified on active adapters:" -ForegroundColor Green
- Write-Host "IPv4 Primary DNS: $IPv4PrimaryDNS"
- Write-Host "IPv4 Secondary DNS: $IPv4SecondaryDNS"
- Write-Host "IPv6 Primary DNS: $IPv6PrimaryDNS"
- Write-Host "IPv6 Secondary DNS: $IPv6SecondaryDNS"
- # Wait for user to read the output before exiting
- Write-Host "`nPress any key to exit..." -ForegroundColor White
- $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement