Created
December 19, 2019 04:57
-
-
Save pepoluan/3e602ae5e556fdb791a42e2f751e7ff4 to your computer and use it in GitHub Desktop.
DNS Switcher Script for PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is released to the Public Domain. | |
# Alternatively, you can use one of the following licenses: Unlicense OR CC0-1.0 OR WTFPL OR MIT-0 OR BSD-3-Clause | |
param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string] $server, | |
[string] $iface = "Wi-Fi", # EDIT AS NEEDED | |
[string] $dns_service = "dnscrypt-proxy" # EDIT AS NEEDED | |
) | |
switch ( $server ) | |
{ | |
"dhcp" { } | |
"office" { $server = 'ENTER YOUR OFFICE DNS SERVER HERE' } # EDIT AS NEEDED | |
"local" { $server = '127.0.0.1' } | |
default { Write-Error "Unknown option '$sel'; choose one of 'office' or 'local' or 'dhcp'!"; exit } | |
} | |
if ($server -eq '127.0.0.1') { | |
Write-Output "Ensuring service '$dns_service' is running" | |
Start-Service $dns_service | |
} | |
Write-Output "Setting DNS for $iface to $server ..." | |
if ($server -eq "dhcp") { | |
Set-DnsClientServerAddress -InterfaceAlias $iface -ResetServerAddresses | |
} else { | |
Set-DnsClientServerAddress -InterfaceAlias $iface -ServerAddresses $server | |
} | |
# "2" (uint16) is the actual ID for the "IPv4" address family | |
# You can verify using this command: | |
# Get-DnsClientServerAddress | where InterfaceAlias -eq "Wi-Fi" | Select-Object -Property AddressFamily,ServerAddresses | |
Get-DnsClientServerAddress | where {$_.InterfaceAlias -eq $iface -and $_.AddressFamily -eq 2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment