← Tillbaka till arkivet
Arkiverat från ITYogi Blogg · 2013-11-06
powershell

Quickly set new DNS servers on a certain subnet

A customer wanted to change and validate the DNS configuration on a subnet so I created this script.

# Script that replaces or sets a random set of DNS servers on every networkadapter with a certain default gateway.
# Version 1.0
# Created by: Lars Gustavsson Knowledge Factory

# Creates two variables to randomly select between.
$0 = "10.0.10.10,10.0.10.11"
$1 = "10.0.10.11,10.0.10.10"

#Getting the IP configuration on all interfaces with a manually configured gateway.
$Interfaces = Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway.NextHop -eq "10.0.10.254" -and $_.IPv4Address.PrefixOrigin -eq "manual"}

#Looping through all the interfaces and setting the DNS servers.
foreach ($interface in $interfaces){
$DNS = Get-Random -InputObject $0, $1
Set-DnsClientServerAddress -InterfaceAlias $interface.InterfaceAlias -ServerAddresses $DNS
}