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

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=@("","")} -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
{Write-host "$Computername is down or need to enable powershell remoting" -b "Red" -foregroundcolor "white"}
remove-pssession *

No comments: