Avatar of Colchester_Institute
Colchester_Institute
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Change DNS using Powershell

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

Powershell

Avatar of undefined
Last Comment
Colchester_Institute

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Colchester_Institute

ASKER
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?
Chris Dent


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
Colchester_Institute

ASKER
why thank

my manager has said hes not fussed bout the wins but it was for my own learning curve.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23