blog
PowerShell - zdalna zmiana ustawień adresów DNS
Czasami trzeba szybko zmienić adresy serwerów DNS na wielu maszynach jednocześnie. Zamiast robić to ręcznie, można użyć prostego skryptu w PowerShellu.
Poniższa funkcja łączy się z wybranym komputerem, znajduje kartę sieciową po nazwie interfejsu i ustawia nowe serwery DNS za pomocą Set-DnsClientServerAddress.
function Set-DnsServerIpAddress {
param(
[string] $ComputerName,
[string] $NicName,
[string[]] $IpAddresses
)
if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
param($ComputerName, $NicName, $IpAddresses)
Write-Host "Setting on $ComputerName on interface $NicName a new set of DNS Servers $IpAddresses"
Set-DnsClientServerAddress -InterfaceAlias $NicName -ServerAddresses $IpAddresses
} -ArgumentList $ComputerName, $NicName, $IpAddresses
} else {
Write-Host "Can't access $ComputerName. Computer is not online."
}
}
Użycie skryptu jest proste: podaj nazwę komputera, nazwę karty sieciowej lub wzorzec nazwy oraz listę adresów DNS, które mają zostać ustawione.
# $ServerName can be set as needed.
# Service is the name of the network card. Wildcards are supported.
# IpAddresses are passed as an array.
Set-DnsServerIpAddress -ComputerName $ServerName -NicName "Service*" -IpAddresses '8.8.8.8', '8.8.4.4', '8.8.8.1'