This script update DNS server ip address on the remote server network cards. The steps are the followings :
- retrieve from AD all the server names
- check if the server is responding to ping test
- list the current network information (IP address and DNS info)
- filter the network cards to update with an IP address like 10.x.x.x
- set the new DNS server ip addresses on the remote network cards (“10.1.1.1″,”10.1.1.2″,”10.1.1.3”). There is a randomize function on this list to have a different order for the primary, secondary,.. DNS servers on each servers
- list the updated network settings
Note : Changing DNS Server list on Windows 7 / 2008 / 2008R2 can create a problem with the DNS record of these clients on the DNS Servers. This problem is described here : http://support.microsoft.com/default.aspx?scid=kb;EN-US;2520155. An update is available to avoid this issue.
Script (with Microsoft Active Directory module loaded : import-module activedirectory) :
import-module activedirectory $server_list = Get-ADComputer -LDAPFilter "(operatingsystem=Windows*Server*)" ForEach($ServerName In $server_list){ $hostname = $ServerName.Name $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$hostname'" | Select-Object StatusCode If ($PingStatus.StatusCode -eq 0){ Write-Host "----------------------------------------------------------------------------" write-host $hostname " : " $nics = Gwmi Win32_NetworkAdapterConfiguration -Comp $hostname | Where{($_.IPEnabled -eq "TRUE")} foreach ($nic in $nics) { if ($nic.IPAddress -like "10.*"){ Write-Host "IP Address : " $nic.IPAddress Write-Host "DNS servers : " $nic.DNSServerSearchOrder $newDNSRandom = ($newDNS = "10.1.1.1","10.1.1.2","10.1.1.3") |Get-Random -Count 3 $x = $nic.SetDNSServerSearchOrder($newDNSRandom) if($x.ReturnValue -eq 0){ $newQueryString = Gwmi Win32_NetworkAdapterConfiguration -Comp $hostname | Where{($_.IPEnabled -eq "TRUE")} foreach ($i in $newQueryString){ if ($i.IPAddress -like "10.*"){ Write-Host "`tSuccessfully Changed DNS Servers on " $hostname Write-Host $hostname " NEW DNS SERVERS : " $newQueryString.DNSServerSearchOrder Write-Host "----------------------------------------------------------------------------" } } } else{ Write-Host "`tFailed to Change DNS Servers on " $hostname Write-Host "----------------------------------------------------------------------------" } } } } }
Script (with Quest Active Directory module) :
$server_list = Get-QADComputer -OSName "Windows*Server*" ForEach($ServerName In $server_list){ $hostname = $ServerName.Name $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$hostname'" | Select-Object StatusCode If ($PingStatus.StatusCode -eq 0){ Write-Host "----------------------------------------------------------------------------" write-host $hostname " : " $nics = Gwmi Win32_NetworkAdapterConfiguration -Comp $hostname | Where{($_.IPEnabled -eq "TRUE")} foreach ($nic in $nics) { if ($nic.IPAddress -like "10.*"){ Write-Host "IP Address : " $nic.IPAddress Write-Host "DNS servers : " $nic.DNSServerSearchOrder $newDNSRandom = ($newDNS = "10.1.1.1","10.1.1.2","10.1.1.3") |Get-Random -Count 3 $x = $nic.SetDNSServerSearchOrder($newDNSRandom) if($x.ReturnValue -eq 0){ $newQueryString = Gwmi Win32_NetworkAdapterConfiguration -Comp $hostname | Where{($_.IPEnabled -eq "TRUE")} foreach ($i in $newQueryString){ if ($i.IPAddress -like "10.*"){ Write-Host "`tSuccessfully Changed DNS Servers on " $hostname Write-Host $hostname " NEW DNS SERVERS : " $newQueryString.DNSServerSearchOrder Write-Host "----------------------------------------------------------------------------" } } } else{ Write-Host "`tFailed to Change DNS Servers on " $hostname Write-Host "----------------------------------------------------------------------------" } } } } }
Set DNS server ip address information on remote servers