Saturday, June 1, 2013

Powershell: Change DNS ip addressess remotely on multiple computers - Part 2

Note: All the testings are performed in lab environment, use them at your risk.

I wrote another script which uses wsman to remotely change DNS ips

http://kunaludapi.blogspot.in/2013/06/change-dns-ip-address-remotely-on.html

This script require you to enable psremoting (powershell required) on remote server and servers should be running windows 2012.



enable-psremoting -force #on remote computer

Set-Item wsman:\localhost\client\trustedhosts * -Force #on remote computer


below is the script can be run from client machine

$Computerlist = get-content "C:\list\servers.txt"
foreach ($computername in $computerlist)
{$result =  get-wmiobject win32_pingstatus -filter "address='$computername'"
if ($result.statuscode -eq 0)
{
$Session = new-pssession -computername $computername
Invoke-command {import-module netadapter} -session $session
Invoke-command { $dns=@("192.168.33.40","192.168.33.7")} -session $session
Invoke-command {get-netadapter -name "vNetwork" | Set-DnsClientServerAddress  -ServerAddresses $dns }  -session $session
Invoke-command {$ips = $(get-netadapter -name "vnetwork" | Get-DnsClientServerAddress| where-object {$_.AddressFamily -eq "2"}).serveraddresses } -session $session
Invoke-command {"$env:computername DNS IPs -- $ips" } -session $session
}
else
{Write-host "$Computername is down or need to enable powershell remoting" -b "Red" -foregroundcolor "white"}
}
remove-pssession *



Powershell: Change DNS ip addressess remotely on multiple computers

Note: All the testings are performed in lab environment, use them at your risk.



Recently we upgraded and replaced DNS server, due to this we had to modify/add extra Dns server IP addresses on servers (around 1200 statically assigned), When I received this activity I got one CSV file containing all the details, where I started activity but got fed up after manually editing 20-25 entries.


I have revised this script on my new blog (Update 1 September 2017)
Powershell: Change DNS IP addresses remotely on multiple computers using CIM & WMI


Here I built this script on my home lab environment, it resembles to my office network but all the name, hostnames and ip addresses are different.



In my lab network, I have at least 2 network cards; only one network card is used on the intranet (connected to switch) and on the entire server this network card is renamed as "vNetwork"

(Here WMI service is used to do modification, it can change IP on windows 2003 server as well also it doesn't require powershell to be installed on remote server) 





Now I wanted to change/replace DNS ip for this vNetwork named adapter only.





You might not have permission to run script you will need to verify script execution policy on the server you going to run this script, and modify policy as below screenshot. (Make sure you are running powershell as administrator)

 

Create a list of servers where dns ip need to be change, I created one and renamed it servers.txt which is kept on one of the server under "c:\list"


Below is the script, copy to notepad file, modify green highlighted area as per you convenience and just execute script. I have renamed that txt file name to change-dns.ps1 and kept on "c:\list" with servers list

 


$Computerlist = get-content "C:\list\servers.txt"
$dnsservers =@(192.168.33.40,"192.168.33.7")

foreach ($computername in $computerlist) {
    $result =  get-wmiobject win32_pingstatus -filter "address='$computername'"
    if ($result.statuscode -eq 0) {
        $remoteNic = get-wmiobject -class win32_networkadapter -computer $computername | where-object {$_.netconnectionID -eq "vNetwork"}
        $index = $remotenic.index
        $DNSlist = $(get-wmiobject win32_networkadapterconfiguration -computer $computername -Filter ‘IPEnabled=true’ | where-object {$_.index -eq $index}).dnsserversearchorder
        $priDNS = $DNSlist | select-object -first 1
        Write-host "Changing DNS IP's on $computername" -b "Yellow" -foregroundcolor "black"
        $change = get-wmiobject win32_networkadapterconfiguration -computer $computername | where-object {$_.index -eq $index}
        $change.SetDNSServerSearchOrder($DNSservers) | out-null
        $changes = $(get-wmiobject win32_networkadapterconfiguration -computer $computername -Filter ‘IPEnabled=true’ | where-object {$_.index -eq $index}).dnsserversearchorder
        Write-host "$computername's Nic1 Dns IPs $changes"
    }
    else {
        Write-host "$Computername is down cannot change IP address" -b "Red" -foregroundcolor "white"
    }
}