PowerShell Programming Windows

Change DNS settings on multiple Windows Servers and Workstations

During several Active Directory migration projects I needed to change the DNS server IP addresses on several computers if the DNS service was installed on the Active Directory Domain Controller. Therefore I wrote a little PowerShell script to connect to remote computer and change the DNS server IP address in the network connection via WMI to the onces specified in the script.

This makes changing the IP address of a DNS server much easier because all Windows systems based on Windows Server 2008 / Vista or newer can be edited by using this PowerShell script.

#####################################################################
##
## (C) 2017 Michael Miklis (michaelmiklis.de)
##
##
## Filename:      Update-DNSServer.ps1
##
## Version:       1.0
##
## Release:       Final
##
## Requirements:  -none-
##
## Description:   Changes the DNS server ip addresses in the
##                network connection on remote servers
##
## This script is provided 'AS-IS'.  The author does not provide
## any guarantee or warranty, stated or implied.  Use at your own
## risk. You are free to reproduce, copy & modify the code, but
## please give the author credit.
##
####################################################################
Set-PSDebug -Strict
Set-StrictMode -version latest


function Update-DNSServer {
    <#
    .SYNOPSIS
    Refresh all environment variables
 
    .DESCRIPTION
    Reads all environement variables from registry for scope user and machine
    and updates the current process environment variables
         
    .EXAMPLE
    Update-Environment
    #>
 
    param (
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$Computer,
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$DNSServers
    )



    if ((Test-Connection $Computer -count 1 -ea 0 -quiet) -eq $true)
    {
        # Get all network adapters with enabled TCP/IP protocol
        $IPEnabledAdapters = get-wmiobject -Computer $Computer Win32_NetworkAdapterConfiguration -filter "ipenabled='true'" 

        # Loop through each network adapter
        foreach ($Adapter in $IPEnabledAdapters) { 
    
            # Check if Adapter has DNS servers configured (to skip Multihomed Cluster Adapters/Servers)
            if ($Adapter.DNSServerSearchOrder -ne $null) {
                    Write-Host ("Configured " + $Adapter.Caption + " with specified DNS Servers")
                    $result = $Adapter.setDNSServerSearchOrder($DNSServers)
                    
                    # if an error occured display the error code
                    if ($result.ReturnValue -ne 0)
                    {
                        Write-Error ("Error occured in setDNSServerSearchOrder() - error code " + $result.ReturnValue)
                    }
             }
         
             # No DNS servers configured for adapter
             else {
                Write-Warning ($Adapter.Caption + " has no DNS Server configured")
             }
        } 
    }

    else
    {
        Write-Warning ("Computer " + $Computer + "does not respond to ping requests - Ignoring")
    }

}
 
#_____________________________________________________________________________________________
 
#call Update-Environment() function
Update-DNSServer -Computer "localhost" -DNSServers @("8.8.8.8","8.8.4.4")

To use this script for multiple computers you can just call the function “Update-DNSServer” in a foreach-loop. Maybe something similar to this:

Import-Module ActiveDirectory

$Computers = Get-ADComputer -SearchBase "dc=domain,dc=local"

foreach ($Computer in $Computers)
{
        Update-DNSServer -Computer $Computer.DNSHostName -DNSServers @("8.8.8.8","8.8.4.4")
}
You may also like
SESSIONID.EXE – Query Terminalserver Session ID
SCCM 2012 Extension: Remove a Computer from all Collections
Set Creation- and Modification-Date from Filename

Leave Your Comment

Your Comment*

Your Name*
Your Webpage