script to change DNS settings - Internet Protocol (TCP/IP) properties

I was wondering if it is possible to make a script to change DNS entries to the following two DNS servers? The other settings such as IP, GT, and SM should stay intact. This change would be on the Internet Protocol (TCP/IP) properties where we have static entries for each server such as IP, SM, DG, Primary DNS Server and Alternative DNS server.
 
-The DNS servers should be changed to the following - for example.  DNS 1: 196.168.1.5. DNS 2: 196.168.6
Even better if I could update all the servers at once if it is possible, but at least it would be great if I have the script to run on each server instead of typing manually the IPs for DNS manually
creative555Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Here's a slightly modified version of that found in Q_28639110.  You need to supply a list of servers (one per line) to work on in the "computers.txt" file.  Assuming you have permissions, this will access the servers remotely.
$servers = get-content "c:\temp\computers.txt"
foreach ($server in $servers) {
    Write-Host "Connect to $server..."
    $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'true' AND DHCPEnabled = 'false'" -ComputerName $server | Where {$_.DnsServerSearchOrder -ne $null}
    $newDNS = "196.168.1.5","196.168.1.6"
 
    foreach ($nic in $nics) {
        Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder
        $x = $nic.SetDNSServerSearchOrder($newDNS)
 
        if($x.ReturnValue -eq 0) {
            Write-Host "`tSuccessfully Changed DNS Servers on $server"
        } else {
            Write-Host "`tFailed to Change DNS Servers on $server"
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
Oh. Wonderful! I will give it a try. What if I just want to run the script locally on the server that I am doing and not multiple servers.

What would that script would be? Could you please remove the additional lines so I have the script for individual/local server as well.

Thank you so much!!
creative555Author Commented:
Excellent! Works perfect!!!!
footechCommented:
BTW, to change it to work locally, just use "localhost" or "." as the server name, or omit the -computername parameter from the Get-WmiObject command.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.