Month: June 2016

Datacenter – Change DNS Server

Let’s not kid ourselfs chaning dns servers happens. There is a new domain controller or someone moves the DNS to a new box with a new IP-address and the pesky job of changing all the primary and secondary DNS entrys on all your member servers has just got dropped into your lap.

Well there is a bright side. You can use PowerShell!

Posted below is a script that will change the primary and secondary dns server entry for a list of computers. The list can be either manual like this invoke-dnsserverchange.ps1 –computername server01.corp.viamonstra.com –primarydns 8.8.8.8 –secondarydns 8.8.4.4 or it could be an txt file with all the server in it like so invoke-dnsserverchange.ps1 c:\myservers.txt –primarydns 8.8.8.8 –secondarydns 8.8.4.4

By default the script will output logfile where it will list all the servers it has tried and notify you on the result for each. The script will not change dnsentries for servers with more than 1 network adapter. This is due to me having no control if you have a server with external dns on one side and internal on the other so those you need to change manually.

Since this requires PowerShell don’t forget that for Server 2008 and R2 you still need to enable and open the firewall for remote PowerShell for this to work.

The Script

<#
Created:     2016-04-15
Version:     1.0
Author :     Peter Lofgren
Twitter:     @LofgrenPeter
Blog   :     https://syscenramblings.wordpress.com

Disclaimer:
This script is provided "AS IS" with no warranties, confers no rights and
is not supported by the author
#>
<#
.SYNOPSIS
  Change DNS Client address on a computer
.DESCRIPTION
  Sets new DNS client ip address on one or more computers
.EXAMPLE
  Invoke-DnsServersChange.ps1 -ComputerName Server01.corp.viamonstra.com -PrimaryDns 8.8.8.8 -SecondaryDNS 8.8.4.4
.EXAMPLE
  Invoke-DnsServersChange.ps1 -ComputerName Server01.corp.viamonstra.com,Server02.corp.viamonstra.com -PrimaryDns 8.8.8.8 -SecondaryDNS 8.8.4.4
.EXAMPLE
  Invoke-DnsServersChange.ps1 -ComputerName C:\Servers.txt -PrimaryDns 8.8.8.8 -SecondaryDNS 8.8.4.4
#>
param (
  [Parameter(Mandatory=$true,Position=0)]
  $ComputerName,
  [Parameter(Mandatory=$true,Position=1)]
  $PrimaryDNS,
  [Parameter(Mandatory=$true,Position=2)]
  $SecondaryDNS,
  [Parameter(Mandatory=$false,Position=3)]
  $LogFile = ".\DnsServers.log"
)
Add-Content -Path $LogFile -Value "Starting DNS set run at $(Get-Date -Format yyy-MM-dd) $(Get-Date -Format HH:mm)" -Force
Add-Content -Path $LogFile -Value "ComputerName,PrimaryDNS,SecondaryDNS,Result" -Force

if ((Test-path -Path $ComputerName) -eq $true) {
  $ComputerName = Get-Content -Path $ComputerName
}

if ($ComputerName.count -eq 1 -and $ComputerName -eq $env:COMPUTERNAME) {
  if ((Get-NetAdapter).count -ge 2) {
    $Result = "$env:COMPUTERNAME,FAILED,FAILED,Multiple Adapters found"
  }
  Else {
    $InterfaceIndex = (Get-NetAdapter -Physical).InterfaceIndex
    Set-DnsClientServerAddress -InterfaceIndex $InterfaceIndex -ServerAddresses $PrimaryDNS,$SecondaryDNS
    $Result = "$env:COMPUTERNAME,$PrimaryDNS,$SecondaryDNS,SUCCESS"
  }
  Add-Content -Path $LogFile -Value $Result -Force
}
Else {
  foreach ($Computer in $ComputerName) {
    if ($Computer -eq $env:COMPUTERNAME) {
      if ((Get-NetAdapter).count -ge 2) {
        $Result = "$env:COMPUTERNAME,FAILED,FAILED,Multiple Adapters found"
      }
      Else {
        $InterfaceIndex = (Get-NetAdapter -Physical).InterfaceIndex
        Set-DnsClientServerAddress -InterfaceIndex $InterfaceIndex -ServerAddresses $PrimaryDNS,$SecondaryDNS
        $Result = "$env:COMPUTERNAME,$PrimaryDNS,$SecondaryDNS,SUCCESS"
      }
      Add-Content -Path $LogFile -Value $Result -Force
    }
    Else {
      $Result = Invoke-Command -ComputerName $Computer -ScriptBlock {
        param (
        $PrimaryDNS,
        $SecondaryDNS
        )
        if ((Get-NetAdapter).count -ge 2) {
          return "$env:COMPUTERNAME,FAILED,FAILED,Multiple Adapters found"
        }
        Else {
          $InterfaceIndex = (Get-NetAdapter -Physical).InterfaceIndex
          Set-DnsClientServerAddress -InterfaceIndex $InterfaceIndex -ServerAddresses $PrimaryDNS,$SecondaryDNS
          return "$env:COMPUTERNAME,$PrimaryDNS,$SecondaryDNS,SUCCESS"
        }
      } -ArgumentList $PrimaryDNS,$SecondaryDNS -ErrorAction SilentlyContinue
      if ($? -eq $false) {
        Add-Content -Path $LogFile -Value "$Computer,FAILED,FAILED,Failed to connect" -Force
      }
      else {
        Add-Content -Path $LogFile -Value $Result -Force
      }
    }
  }
}
Add-Content -Path $LogFile -Value "Finished DNS set run at $(Get-Date -Format yyy-MM-dd) $(Get-Date -Format HH:mm)"

 

Happy deploying!
Peter