Files
winutil/functions/private/Set-WinUtilDNS.ps1
T

34 lines
1.3 KiB
PowerShell
Raw Normal View History

+3
2023-03-07 12:28:00 -08:00
function Set-WinUtilDNS {
<#
2023-10-19 17:12:55 -05:00
.SYNOPSIS
Sets the DNS of all interfaces that are in the "Up" state. It will lookup the values from the DNS.Json file
+3
2023-03-07 12:28:00 -08:00
2023-10-19 17:12:55 -05:00
.PARAMETER DNSProvider
The DNS provider to set the DNS server to
.EXAMPLE
+3
2023-03-07 12:28:00 -08:00
Set-WinUtilDNS -DNSProvider "google"
2023-10-19 17:12:55 -05:00
+3
2023-03-07 12:28:00 -08:00
#>
param($DNSProvider)
if($DNSProvider -eq "Default") {return}
try {
+3
2023-03-07 12:28:00 -08:00
$Adapters = Get-NetAdapter | Where-Object {$_.Status -eq "Up"}
Write-Host "Ensuring DNS is set to $DNSProvider on the following interfaces"
Write-Host $($Adapters | Out-String)
Foreach ($Adapter in $Adapters) {
if($DNSProvider -eq "DHCP") {
+3
2023-03-07 12:28:00 -08:00
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ResetServerAddresses
} else {
+3
2023-03-07 12:28:00 -08:00
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ServerAddresses ("$($sync.configs.dns.$DNSProvider.Primary)", "$($sync.configs.dns.$DNSProvider.Secondary)")
2024-07-09 01:32:03 +02:00
Set-DnsClientServerAddress -InterfaceIndex $Adapter.ifIndex -ServerAddresses ("$($sync.configs.dns.$DNSProvider.Primary6)", "$($sync.configs.dns.$DNSProvider.Secondary6)")
+3
2023-03-07 12:28:00 -08:00
}
}
} catch {
+3
2023-03-07 12:28:00 -08:00
Write-Warning "Unable to set DNS Provider due to an unhandled exception"
2023-10-19 17:12:55 -05:00
Write-Warning $psitem.Exception.StackTrace
+3
2023-03-07 12:28:00 -08:00
}
}