• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 79
  • Last Modified:

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
0
GGHC
Asked:
GGHC
  • 5
  • 3
  • 3
1 Solution
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
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 Specialist/DeveloperCommented:
Do you only have 24-bit subnets?
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
Shaun VermaakTechnical Specialist/DeveloperCommented:
$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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now