Script to Set DNS 1&2 based on Nic IP address

We have over 20 subnets . Each Subnet will have a different DNS Config.
All servers have a Static IP/DNS.  
I have a script below to set the DNS on the servers but Want to enhance it so it will be a single script and based on first 3 Octets of the "Ethernet" adapter, It will assign the specific DNS.  

Sample Subnets >DNS
172.120.8.x =DNS "172.120.8.150" "172.120.9.150"
172.120.9.x =DNS "172.120.9.150" "172.120.8.150"
172.120.16.x =DNS "172.120.16.150" "172.120.17.150"
172.120.17.x =DNS "172.120.17.150" "172.120.16.150"

~~Current Script~~~
#Variables
$DNS1 = "172.120.16.150"
$DNS2 = "172.120.17.150"
$Adapter1 = Get-NetAdapter "Eth*" | select -expand name

#Assigning primary and secondary DNS servers
netsh interface ipv4 set dnsservers name=$Adapter1 static $DNS1 primary
netsh interface ipv4 add dnsservers name=$Adapter1 $DNS2 index=2
Write-Host "The Primary DNS is:" $DNS1
Write-Host "The Secondary DNS is:" $DNS2
exit
GGHCAsked:
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.

Shaun VermaakTechnical SpecialistCommented:
Any reason why you are not using Set-DnsClientServerAddress?
0
GGHCAuthor Commented:
I try it but at first shot ended up setting different Interface.

But the command/script to set DNS is not what I need help on since that script works fine.
It's getting the script to set specific DNS entries based on the first 3 Octet of the Host IP. That's the part I am stumbling on.
0
Shaun VermaakTechnical SpecialistCommented:
Do you only have 24-bit subnets?
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Shaun VermaakTechnical SpecialistCommented:
$IPAddress = "192.168.0.101"
$IPOctets = $IPAddress.Split(".")
$NetworkID = ($IPOctets[0]+"."+$IPOctets[1]+"."+$IPOctets[2])

Open in new window

0
GGHCAuthor Commented:
Shaun,
The 3rd Octet will be different. That 3rd Octet will dictate what DNS will be configured for it.
It will be something like:

IF IP on Ethernet* = "172.120.8.*" Then goto Subnet8
IF IP on Ethernet* = "172.120.9.*" Then goto Subnet9
:Subnet8
~~~Script to assign for this subnet~~~

:Subnet9
~~~Script to assign for this subnet~~~


I need help on script command to get the IP of Ethernet and using the result to trigger the GOTO part of the script.
0
Jason CrawfordTransport NinjaCommented:
Try this:

$ips = Get-NetIPAddress -InterfaceAlias '*ethernet*' -IPAddress '172.120.*'

foreach ($ip in $ips) {
    if ($ip.IPAddress -like '172.120.8.*') {
        Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.8.150','172.120.9.150'
    }
    elseif ($ip.IPAddress -like '172.120.9.*') {
        Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.16.150','172.120.17.150'
    }
    elseif ($ip.IPAddress -like '172.120.17.*') {
        Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.17.150','172.120.16.150'
    }
}

Open in new window

This assumes that the script is run on an individual server with one or more interfaces named Ethernet.  If you want to deploy the script to multiple servers we can leverage PowerShell Remoting or something similar.
1

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
GGHCAuthor Commented:
NICE! Worked perfectly!
0
Jason CrawfordTransport NinjaCommented:
Glad I could help.  Take care :)
1
GGHCAuthor Commented:
Jason,
Hummm...
Thinking about the Powershell Remoting you mentioned. If I have a list of computer in file how can I use that to deploy these settings?
0
Jason CrawfordTransport NinjaCommented:
Open Notepad and save the hostname of each server you want to run the script on one name per line so something like:

server1
server2
server3

Save the file as servers.txt on the desktop you want to deploy the script from and run this:

Invoke-Command -ComputerName (Get-Content "$env:USERPROFILE\Desktop\servers.txt") -ScriptBlock {
    $ips = Get-NetIPAddress -InterfaceAlias '*ethernet*' -IPAddress '172.120.*'

    foreach ($ip in $ips) {
        if ($ip.IPAddress -like '172.120.8.*') {
            Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.8.150','172.120.9.150'
        }
        elseif ($ip.IPAddress -like '172.120.9.*') {
            Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.16.150','172.120.17.150'
        }
        elseif ($ip.IPAddress -like '172.120.17.*') {
            Set-DnsClientServerAddress -InterfaceAlias $ip.InterfaceAlias -ServerAddresses '172.120.17.150','172.120.16.150'
        }
    }
}

Open in new window

That's a good start assuming PSRemoting is enabled on the remote servers.  If you run into remoting errors check this out:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enable-psremoting?view=powershell-5.1
1
GGHCAuthor Commented:
Jason,
Circling back because now I need to include 192.168.1.* and 192,168.2.*  to line below. How do I include it?


$ips = Get-NetIPAddress -InterfaceAlias '*ethernet*' -IPAddress '172.120.*'
0
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
Powershell

From novice to tech pro — start learning today.