Skip to content

Instantly share code, notes, and snippets.

@bluPhy
Last active August 31, 2021 11:11
Show Gist options
  • Select an option

  • Save bluPhy/70c54f52d088fde471d32bf8601b6dfc to your computer and use it in GitHub Desktop.

Select an option

Save bluPhy/70c54f52d088fde471d32bf8601b6dfc to your computer and use it in GitHub Desktop.
Menu based PowerShell script to change DNS servers for local area connections
#
# Menu based Powershell script to change DNS servers for local area connections.
#
$DNSServer = @{"Home1" = "192.168.2.2";
"Home2" = "192.168.2.10";
"Home3" = "192.168.2.1";
"OpenDNS1" = "208.67.220.220";
"OpenDNS2" = "208.67.222.222";
"Google1" = "8.8.8.8";
"Google2" = "8.8.4.4"
}
function Use-RunAs {
# Check if script is running as Adminstrator and if not use RunAs
# Use Check Switch to check if admin
param([Switch]$Check)
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($Check) { return $IsAdmin }
if ($MyInvocation.ScriptName -ne "") {
if (-not $IsAdmin) {
try {
$arg = "-file `"$($MyInvocation.ScriptName)`""
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch {
Write-Warning "Error - Failed to restart script with runas"
break
}
exit # Quit this session of powershell
}
}
else {
Write-Warning "Error - Script must be saved as a .ps1 file first"
break
}
}
# Check if script is running as Adminstrator and if not use RunAs
Use-RunAs
"Script Running As Administrator"
Start-Sleep 3
$MenuDNSChange = {
Write-Host " ******************************************************************" -ForegroundColor Cyan
Write-Host " * DNS Client Server Change menu *" -ForegroundColor Cyan
Write-Host " * *" -ForegroundColor Cyan
Write-Host " ******************************************************************" -ForegroundColor Cyan
Write-Host " "
Write-Host " Please select an option from the list below:" -foregroundcolor green
Write-Host " "
Write-Host " 0) Show current client DNS settings" -ForegroundColor White
Write-Host " "
Write-Host " 1) Configure DNS Settings for Home" -ForegroundColor White
Write-host " Servers: " $DNSServer.Home1, $DNSServer.Home2, $DNSServer.Home3 -ForegroundColor White
Write-Host " "
Write-Host " 2) Configure DNS Settings to OpenDNS " -ForegroundColor White
Write-host " Servers: " $DNSServer.OpenDNS1, $DNSServer.OpenDNS2 -ForegroundColor White
Write-Host " "
Write-Host " 3) Configure DNS to DHCP for a NIC" -ForegroundColor White
Write-Host " 4) Configure DNS to DHCP for all NICs" -ForegroundColor White
Write-Host " "
Write-Host " 5) Enable/Disable a NIC" -ForegroundColor White
Write-Host " ------" -ForegroundColor White
Write-Host " "
Write-Host " 98) Restart the Server" -ForegroundColor Red
Write-Host " 99) Exit" -ForegroundColor Cyan
Write-Host " "
Write-Host " Select an option.. [1-99]? " -nonewline
}
Do {
if ($Choice -ne "None") { Write-Host "Last command: "$Choice -foregroundcolor Yellow }
invoke-command -scriptblock $MenuDNSChange
$Choice = Read-Host
switch ($Choice) {
0 {
# Show current client DNS settings
Write-Host "Current DNS Servers:"
Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $($_.ServerAddresses) }
Write-Host " "
Get-DnsClientServerAddress -AddressFamily IPv6 | Where-Object { $($_.ServerAddresses) }
Write-Host " "
}
1 {
# Configure DNS Settings for Home
Get-NetAdapter -Physical | Format-Table ifIndex, Name, InterfaceDescription, Status
$InterfaceChoice = Read-Host "Interface Index ?"
Set-DnsClientServerAddress -InterfaceIndex $InterfaceChoice -ServerAddresses ($DNSServer.Home1, $DNSServer.Home2, $DNSServer.Home3)
Clear-DnsClientCache
$InterfaceChoice = $null
}
2 {
# Configure DNS Settings to OpenDNS
Get-NetAdapter -Physical | Format-Table ifIndex, Name, InterfaceDescription, Status
$InterfaceChoice = Read-Host "Interface Index ?"
Set-DnsClientServerAddress -InterfaceIndex $InterfaceChoice -ServerAddresses ($DNSServer.OpenDNS1, $DNSServer.OpenDNS2)
Clear-DnsClientCache
$InterfaceChoice = $null
}
3 {
# Configure DNS to DHCP for a single NIC
Get-NetAdapter -Physical | Format-Table ifIndex, Name, InterfaceDescription, Status
$InterfaceChoice = Read-Host "Interface Index ?"
Set-DnsClientServerAddress -InterfaceIndex $InterfaceChoice -ResetServerAddresses
Clear-DnsClientCache
$InterfaceChoice = $null
}
4 {
# Configure DNS to DHCP for all NICs
$AllNetAdapters = (Get-NetAdapter -Physical)
Foreach ($NetAdapter in $AllNetAdapters) {
Write-Host "Resetting DNS for NIC: " $NetAdapter.Name
Set-DnsClientServerAddress -InterfaceIndex $NetAdapter.ifIndex -ResetServerAddresses
}
Clear-DnsClientCache
}
5 {
# Enable/Disable a Physical NIC
Get-NetAdapter -Physical | Format-Table ifIndex, Name, InterfaceDescription, Status
$InterfaceChoice = Read-Host "Interface Index ?"
$InterfaceName = (Get-NetAdapter -InterfaceIndex $InterfaceChoice).Name
$InterfaceStatus = (Get-NetAdapter -InterfaceIndex $InterfaceChoice).Status
If ($InterfaceStatus -like "Disabled") { Write-Host "Enabling NIC... " ; Enable-NetAdapter -Name $InterfaceName } Else { Write-Host "Disabling NIC... " ; Disable-NetAdapter -Name $InterfaceName }
Clear-DnsClientCache
$InterfaceChoice = $null
$InterfaceName = $null
$InterfaceStatus = $null
}
98 {
# Exit and restart
[ValidateSet('Yes', 'No')]$VerifyReboot = Read-Host "Are you sure you want to restart this computer?"
If ($VerifyReboot -like "yes") { Write-Host "Rebooting... " ; Restart-Computer -ComputerName localhost -Force }
}
99 {
# Exit
Pop-Location
Write-Host "Exiting..."
}
default { Write-Host "You haven't selected any of the available options. " }
}
} while ($Choice -ne 99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment