Powershell – Zdalna zmiana ustawień adresów DNS
Czasami potrzebujemy szybko zmienić ustawienia adresów serwerów DNS na wielu maszynach jednocześnie. By nie robić tego ręcznie można napisać prosty skrypt w PowerShellu, który zrobi to za nas.
Rozwiązanie
Poniżej znajduje się prosty skrypt, który znajduje karte sieciową po jej nazwie i ustawia nowe serwery DNS za pomocą funkcji PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 | function Set-DnsServerIpAddress([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 stosunkowo proste:
1 2 3 4 | # Where $ServerName can be set as needed # Where Service is the name of the Network Card (takes wildcard) # Where IpAddresses are given in brackets Set-DnsServerIpAddress -ComputerName $ServerName -NicName "Service*" -IpAddresses '8.8.8.8','8.8.4.4','8.8.8.1' |