[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

Set DNS and IP information on servers with multiple Nics

I have found many a script that will set DNS and WINS:
function Set-DNSWINS {
#Get NICS via WMI
$NICs = Get-WmiObject '
-Class Win32_NetworkAdapterConfiguration '
-ComputerName $_ '
-Filter "IPEnabled=TRUE"

foreach($NIC in $NICs) {
$DNSServers = "12.34.5.67","76.54.3.21"
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration("TRUE")
$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
}
}

Open in new window


However my servers have many nic's and Clustered nics, etc...  

Is there a way to run a powershell script and set the DNS/WINS Servers only on the Nic with the Default gateway set?  Then after it is set, remove any DNS/Wins servers on nics that don't have default gateways?

Additoinally I have 4 sets of DNS Servers for 4 networks.

is it possible for
1.1.1.0 subnet to use DNS Servers 2.2.2.2 .2.2.2.3
2.2.2.0 subment to use dns servers 11.11.11.11 11.11.11.12

Thanks
0
brittonv
Asked:
brittonv
  • 2
  • 2
1 Solution
 
Neil RussellTechnical Development LeadCommented:
"Is it possible for
1.1.1.0 subnet to use DNS Servers 2.2.2.2 .2.2.2.3
2.2.2.0 subment to use dns servers 11.11.11.11 11.11.11.12"

In a word No.

DNS is NAME to IP Resolution on the computer/server. So until it resolves a name how does it know what subnet to look at in your situation.

Can you explain what you want to achieve?

Is this your DNS Server?
0
 
chrismerrittCommented:
How does this work? please test it first!

function Set-DNSWINS
{
	#Get NICS via WMI
	$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter "IPEnabled=TRUE"

	#Loop through NICs
	foreach($NIC in $NICs)
	{
		#Check that the NIC has a default Gateway Set
		if ($NIC.DefaultIPGateway -ne $Null)
		{
			
			Write-Host -ForeGroundColor "magenta" "NIC with Default gateway found $($NIC.Description)"
			
			#Use the Switch statement to set the DNS servers based on the IP address.
			#Hope for the best that the first IP in the IPAddress Array is the IPv4 address :D
			switch ($NIC.IPAddress[0])
			{
				{$NIC.IPAddress[0] -match "1.1.1"}
				{
					write-host -ForeGroundColor "Yellow" "IP Address matched 1.1.1"
					$DNSServers = "2.2.2.2","2.2.2.3"
					$NIC.SetDNSServerSearchOrder($DNSServers)
					$NIC.SetDynamicDNSRegistration("TRUE")
					$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
				}
				{$NIC.IPAddress[0] -match "2.2.2"}
				{
					write-host -ForeGroundColor "Yellow" "IP Address matched 2.2.2"
					$DNSServers = "11.11.11.11","11.11.11.12"
					$NIC.SetDNSServerSearchOrder($DNSServers)
					$NIC.SetDynamicDNSRegistration("TRUE")
					$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
				}
				#Can add more switch conditions here.
			}
		}
	}
}

Open in new window

0
 
brittonvAuthor Commented:
that is hella sweet!


IN your Default gateway set, could I add someting with an "Else" to clean up entries for nics where the Gateway IS null?

0
 
brittonvAuthor Commented:
Oh and if so, what would that else statement look like?
0
 
chrismerrittCommented:
Try this? not sure what you want to do with the non default gateway NIC's but this will echo them back to you:

function Set-DNSWINS
{
	#Get NICS via WMI
	$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter "IPEnabled=TRUE"

	#Loop through NICs
	foreach($NIC in $NICs)
	{
		#Check that the NIC has a default Gateway Set
		if ($NIC.DefaultIPGateway -ne $Null)
		{
			
			Write-Host -ForeGroundColor "magenta" "NIC with Default gateway found $($NIC.Description)"
			
			#Use the Switch statement to set the DNS servers based on the IP address.
			#Hope for the best that the first IP in the IPAddress Array is the IPv4 address :D
			switch ($NIC.IPAddress[0])
			{
				{$NIC.IPAddress[0] -match "1.1.1"}
				{
					write-host -ForeGroundColor "Yellow" "IP Address matched 1.1.1"
					$DNSServers = "2.2.2.2","2.2.2.3"
					$NIC.SetDNSServerSearchOrder($DNSServers)
					$NIC.SetDynamicDNSRegistration("TRUE")
					$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
				}
				{$NIC.IPAddress[0] -match "2.2.2"}
				{
					write-host -ForeGroundColor "Yellow" "IP Address matched 2.2.2"
					$DNSServers = "11.11.11.11","11.11.11.12"
					$NIC.SetDNSServerSearchOrder($DNSServers)
					$NIC.SetDynamicDNSRegistration("TRUE")
					$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
				}
				#Can add more switch conditions here.
			}
		}
		else
		{
			write-host -ForeGroundColor "Red" "This NIC has no Default Gateway set $($NIC.Description)"
		}
	}
}

Open in new window

0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

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