Fun with Powershell - How to change the server DNS and GATEWAY remotely using Powershell

I have started to indulge myself with Powershell lately and this is a pretty handy script that I came up with, I wanted to change the dns servers and the gateway for some of my test servers remotely, so I was looking at how I could do it with my new found abilities!

Its a pretty simple script, nothing fancy and I am sure there are similar ones on the internet! So here goes..

$servers =  Get-Content -Path C:\Mobaterm\repo\servers.txt
$dnsservers = "10.0.0.1","10.0.0.2"
$gateway = "10.0.0.3"
$nics = Get-WmiObject win32_networkadapterconfiguration -ComputerName $servers -Filter "ipenabled = true"
foreach ($nic in $nics){
$nic.setdnsserversearchorder($dnsservers)
$nic.setgateways($gateway)
}

As you can see above, I have stored my server list in a file called servers.txt and called that in a variable called $servers. Then I create different variable for the dns servers that I want to modify with the ip addresses and a new variable for the gateway.

Then I retrieve the active network card information using a WMI class and start a for loop to iterate through each item and modify it. Like I said pretty simple nothing out of the ordinary.

-Alstar