如何为包含特定ConnectionSpecificSuffix的所有NICS更改DNS服务器?

时间:2019-03-18 20:35:41

标签: powershell

我目前可以在计算机列表中为所有NICS更改DNS服务器。

$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”)
}

如何仅更改连接到特定域名的NIC的DNS服务器?有些计算机可能具有多个NIC,我只想更改连接到“ mytestdomain.local”的NICS的DNS设置。

通过阅读PowerShell示例,看起来ConnectionSpecificSuffix显示了域名。 (-ConnectionSpecificSuffix“ mytestdomain.local”)

是否可以编辑脚本并仅更改后缀为“ mytestdomain.local”的NIC的DNS设置?

这不是:

$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”)
}

2 个答案:

答案 0 :(得分:1)

Win32_NetworkAdapterConfiguration实例没有ConnectionSpecificSuffix成员。但是,它们确实具有DNSDomainDNSDomainSuffixSearchOrder属性。

因此,我认为您正在寻找的是这个...

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer `
    | where { $_.IPEnabled -eq "TRUE" -and $_.DNSDomain -eq "mytestdomain.local" }

或者,如果您想测试目标域是否在任何搜索后缀中,无论顺序如何,那么您都可以这样做...

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer `
    | where { $_.IPEnabled -eq "TRUE" -and $_.DNSDomainSuffixSearchOrder -contains "mytestdomain.local" }

顺便说一句,这未经测试,但是如果您的Windows版本足够新,则可以使用dnsclient module将代码简化为类似这样的内容...

Get-DnsClient -ConnectionSpecificSuffix 'mytestdomain.local' `
    | Set-DnsClientServerAddress -ServerAddresses '10.1.140.3', '10.1.140.4'

使用-RegisterThisConnectionsAddress参数调用Set-DnsClient似乎与调用SetDynamicDNSRegistration的作用相同。

答案 1 :(得分:1)

有一些预构建的脚本可将您带到您要去的地方。根据需要柚木。例如:

  

Set-DNSServerSearchOrder

     

有时,您需要在DNS服务器上设置DNS服务器搜索顺序   没有从DHCP接收IP的计算机。该功能将   让你做到这一点。验证DNS的当前设置   服务器搜索顺序,可以运行以下Get-WmiObject命令   然后选择

     

下载: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
    }
}

使用CIM查看/修改信息。利用这个...

$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