Thursday, September 17, 2020

Powershell: Change static DNS values on the fly

Problem = you have a metric crap ton of statically IP assigned windows machines, and you want to change their DNS settings. 

 I've seen much of this info posted elsewhere, but it feels like no one really puts it all together very well, so here's my own take on how to pull this particular trick off. 

First off, the commands needed to "set" new DNS values, want to know which "adapter" on a machine you're going to change, this is returned as a number value with a name of "interfaceindex". That number VARIES per machine. 

So the following can be run per machine, which will quickly find the proper adapter to adjust and adjust it:


******************Begin script********************

$adapter=get-netadapter | select -expandproperty interfaceindex

$newdns1="1.1.1.1"

$newdns2="2.2.2.2"

set-dnsclientserveraddress -interfaceindex $adapter -serveraddresses ($newdns1,$newdns2)

****************End script***********************


run interactively, these command would require elevation to execute, so plan for that in your deployment. 

This change is immediate, with no event log errors generated, or service outages caused.