使用Powershell更改DNS搜索顺序,并在不同服务器上使用不同的设置

时间:2014-03-27 17:09:03

标签: arrays powershell syntax dns wmi

亲爱的Powershell领主:)

我有380台服务器要修改。 林和域中的某些DNS服务器已更改,我需要更改Dns搜索顺序参数。

如果我的服务器有以下DNSsearchOrder:172.xx.xx.1我想更改为172.xx.xx.2 如果我的服务器有以下DNSsearchOrder:172.xx.xx.1 AND 172.xx.xx.10我想只将第一个IP更改为172.xx.xx.2并让第2个IP 等等(我有10dns服务器移动,他们在不同的域和服务器上使用)

使用以下命令检索我的所有服务器导出到CSV文件:

  Import-csv "C:\Julien\Script\CheckNchangeRegKeyValue\listOfcomputerOS_OK.csv" -Delimiter ";" | 
    Foreach-object{
    $ServerToAnalyse=$_.Server
        Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "ipenabled='true'" -ComputerName $ServerToAnalyse |Where-Object {$_.DNSServerSearchOrder -ne $null}| Where-Object {$_.DNSServerSearchOrder -ine "127.0.0.1"} |`
         select DNSHostName,@{Name="DNSParam"; Expression={$(($_.DNSServerSearchOrder))}},MACAddress,description,caption,index
} | Export-csv "C:\Julien\Script\CheckNchangeRegKeyValue\ListOfDNSSettingsAndHostName.csv" -notypeinformation -delimiter ";"

第一个文件是csv包含的数据:

"Caption";"Server"
"Microsoft(R) Windows(R) Server 2003, Standard Edition";"d0001doi.Mydomain.com"
"Microsoft(R) Windows(R) Server 2003, Standard Edition";"d0001ira.Mydomain.com"
"Microsoft Windows Server 2008 R2 Standard ";"D0005DLF.Mydomain.com"
"Microsoft(R) Windows(R) Server 2003, Standard Edition";"d0001ath.Mydomain.com"
"Microsoft Windows Server 2008 R2 Enterprise ";"D0002TEH.Mydomain.com"
"Microsoft Windows Server 2008 R2 Standard ";"D0003MAD.Mydomain.com"

输出CSV如下:

    "DNSHostName";"DNSParam";"MACAddress";"description";"caption";"index"
"v1068bel";"172.19.1.3";"xx:xx:xx:xx:xx";"VMware Accelerated AMD PCNet Adapter";"[00000010] VMware Accelerated AMD PCNet Adapter";"10"

输出文件包含实际设置,我的想法是使用以下命令修改此文件,以使用此输出文件修改我的服务器的设置:

[io.file]::readalltext("C:\Julien\Script\CheckNchangeRegKeyValue\ListOfDNSSettingsAndHostName.csv").replace("172.19.1.1","172.19.16.211").replace("172.18.37.8","172.19.17.52").replace("172.19.1.5","172.19.16.211").replace("172.19.1.3","172.19.16.28").replace("172.18.37.4","172.19.16.28").replace("172.19.1.6","172.19.16.27").replace(" ",",") | Out-File "C:\Julien\Script\CheckNchangeRegKeyValue\ListOfDNSSettingsAndHostName.csv" -Encoding ascii –Force

在此修改之后,我尝试使用以下命令:

  Import-Csv "C:\Julien\Script\CheckNchangeRegKeyValue\ListOfDNSSettingsAndHostName.csv" -Delimiter ";" | 
ForEach-Object{
$Server=$_.DNSHostName
$DNS=$_.DNSParam
$DNS=$DNS -replace "$DNS","`"$DNS`""
$DNS=($DNS -replace ",","`",`"")#.tostring()
$DNSIP=@("$DNS")
#$DNS=@($DNS)
#$DNS.GetType().fullname
#= New-Object System.Object

Write-Host "settings to put : $DNS"

(Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server).SetDNSServerSearchOrder(("$DNSIP"))#|Out-Null

$ActualDNS = (Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server)|Where-Object {$_.DNSServerSearchOrder -ne $null}| Where-Object {$_.DNSServerSearchOrder -ine "127.0.0.1"} |select @{Name="actualSettings"; Expression={$(($_.DNSServerSearchOrder))}}
Write-Host "actual settings $ACtualDNS"
}

如果我手动SetDNSServerSearchOrder((“198.102.234.125",”198.102.234.126"))而不是.SetDNSServerSearchOrder(("$DNSIP")) 它正在运作

但是使用$ dnsip变量,结果是70(错误的IP地址)

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ReturnValue      : 70
PSComputerName   : 

actual settings @{actualSettings=172.19.1.1}
谁能帮帮我? 非常感谢

此致 于连

1 个答案:

答案 0 :(得分:0)

根据this article,您确实需要一个字符串数组,因此您应该$DNSIP=@("$DNS")替换$DNSIP.split(',')

说明:当您编写“198.102.234.125",”198.102.234.126"时,您创建了一个数组,因为在PowerShell中,是数组运算符。 $DNSIP=@("$DNS")只创建一个不是IP地址的字符串数组。