Solved

Change DNS using Powershell

Posted on 2010-11-16
4
1,109 Views
Last Modified: 2012-05-10
Hi there.

I've managed to find the attached powershell script that allows me to change dns setting on a server.

If works fine except that i can only add two addresses.

I'm looking to add more than that within the advanced setting on the nic.  how do i go about this?
FUNCTION Set-DNSWINS {
#Get NICS via WMI
$Nics = get-WMIObject -class Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter "IPEnabled=TRUE"

foreach ($NIC in $NICs){
$DNSSERVERS = "xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx"
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration("True")
$NIC.SetWINSServer.count("xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx") 
}
}

function Get-filename{
$computer = "e:\Txt_Files\dns.txt"
return $Computer
}

$f = Get-Filename
get-content $f | foreach {set-DNSWINS}

Open in new window

0
Comment
Question by:Colchester_Institute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34144600

Hmm why do you think you can only set two?

This should work:
$DNSServers = "1.2.3.4", "2.3.4.5", "3.4.5.6"
$NIC.SetDNSServerSearchOrder($DNSServers)

Open in new window

There's no reason to limit the number there to two.

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34144667
ahh yes that works...

it was my own fault also where i'd played around with the syntax

it doesnt however let me put more than two wins addresses in...is there a way out of curiosity i can do that?
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34144887

Unfortunately the SetWINSServer method is a bit limited, NetSh should be able to do it, but I can't seem to get the syntax right for more than one address.

The last resort is hitting the registry, WINS addresses hide here:

HKLM:\System\CurrentControlSet\services\NetBT\parameters\Interfaces\Tcpip_<YourInterface>

There's a NameServerList beneath that. We can set that like this:
$DnsServers = "xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx"
[String[]]$WinsServers = "yyy.yyy.yyy.yyy", "yyy.yyy.yyy.yyy", "yyy.yyy.yyy.yyy"

Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$True" | ForEach-Object {
  $_.SetDNSServerSearchOrder($DNSServers)
  $_.SetDynamicDNSRegistration($True)

  Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\services\NetBT\parameters\Interfaces\Tcpip_$($_.SettingID)" `
    -Name "NameServerList" -Value $WinsServers
}

Open in new window

Note that we have to force $WinsServers to be a String array (String[]) or it'll tell us to get lost.

A bit more convoluted that I prefer, but it should work :)

HTH

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34145140
why thank

my manager has said hes not fussed bout the wins but it was for my own learning curve.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question