Reputation: 1181
I can currently change DNS servers for all NICS on a list of computers.
$computer = gc C:\computerlist.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | where { $_.IPEnabled -eq "TRUE"}
ForEach($NIC in $NICs) {
$DNSServers = "10.1.140.3", "10.1.140.4" # CHANGE THIS TO THE IP YOU WANT TO USE
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
}
How do I only change DNS servers for NICs that are connected to a certain domain name? Some computers may have more than one NIC and I only want to change DNS settings for NICS connected to "mytestdomain.local".
From reading PowerShell examples, it looks like the ConnectionSpecificSuffix shows the domain name. (-ConnectionSpecificSuffix "mytestdomain.local")
Is there a way to edit my script and only change DNS settings for NICs that contain the suffix "mytestdomain.local"?
This doesn't :
$computer = gc C:\computerlist.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | where { $_.IPEnabled -eq "TRUE" -and $_.ConnectionSpecificSuffix -eq "mytestdomain.local"}
ForEach($NIC in $NICs) {
$DNSServers = "10.1.140.3", "10.1.140.4" # CHANGE THIS TO THE IP YOU WANT TO USE
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
}
Upvotes: 1
Views: 1673
Reputation: 16106
There are pre-built scripts to get you to where you are trying to go. Teak them as needed. For example:
At times it is necessary to set the DNS Server Search Order on a machine that is not receiving it's IP from DHCP. This function will allow you to do just that. To verify the current setup of the DNS Server Search Order, you can run the following Get-WmiObject command and select
Download: Set-DNSServerSearchOrder.ps1
# Validate DNS Search order
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName "Server01" -Filter "IPEnabled=TRUE" | Select PSComputerName,DNSServerSearchOrder
# or using the DNS cmdlets to first collect the info
(DnsClientGlobalSetting).SuffixSearchList -match $env:USERDNSDOMAIN
Get-DnsClientServerAddress -AddressFamily IPv4 | Select *
(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses
# Modify DNS search order
function Set-DNSServerSearchOrder
{
Param
(
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
$ComputerName=$Env:ComputerName,
[String[]]$DNSServers = @("10.10.10.1","10.10.10.2")
)
$getWmiObjectSplat = @{
Filter = "IPEnabled=TRUE"
ComputerName = $ComputerName
Class = 'Win32_NetworkAdapterConfiguration'
}
$NICs = Get-WmiObject @getWmiObjectSplat
foreach($NIC in $NICs)
{
$NIC.SetDNSServerSearchOrder($DNSServers) |
out-null
}
}
Looking at / modifying the info using CIM instead. Leverage this...
$nics = [wmiclass]'win32_Networkadapterconfiguration'
[wmiclass]'Win32_NetworkAdapterConfiguration'
$nics.GetMethodParameters("SetDNSSuffixSearchOrder")
$nics | select -ExpandProperty Properties
DNSDomainSuffixSearchOrder
DNSServerSearchOrder
$cimClass = Get-CimClass win32_Networkadapterconfiguration
$cimClass.CimClassMethods
$cimClass.CimClassMethods["SetDNSSuffixSearchOrder"].Parameters
$cimClass.CimClassMethods["EnableDNS"].Parameters
$cimClass.CimClassMethods["EnableDNS"].Qualifiers
Get-WmiObject -Class win32_Networkadapterconfiguration
Upvotes: 1
Reputation: 16612
Win32_NetworkAdapterConfiguration
instances do not have a ConnectionSpecificSuffix
member. They do, however, have DNSDomain
and DNSDomainSuffixSearchOrder
properties.
Thus, I think what you're looking for is this...
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer `
| where { $_.IPEnabled -eq "TRUE" -and $_.DNSDomain -eq "mytestdomain.local" }
Alternatively, if you want to test if the target domain is among any of the search suffixes, regardless of order, then you'd do this...
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer `
| where { $_.IPEnabled -eq "TRUE" -and $_.DNSDomainSuffixSearchOrder -contains "mytestdomain.local" }
By the way, this is untested but if your Windows version is new enough you could use the dnsclient
module to simplify your code to something like this...
Get-DnsClient -ConnectionSpecificSuffix 'mytestdomain.local' `
| Set-DnsClientServerAddress -ServerAddresses '10.1.140.3', '10.1.140.4'
It looks like calling Set-DnsClient
with the -RegisterThisConnectionsAddress
parameter might do the same thing as calling SetDynamicDNSRegistration
.
Upvotes: 1