Question

Hi dear lord(s) of Powershell :)

I have 380 server to modify. Some DNS server in forest and domain have changed and I need to change the Dns search order parameter.

If my server have following DNSsearchOrder : 172.xx.xx.1 I want to change to 172.xx.xx.2 If my server have following DNSsearchOrder : 172.xx.xx.1 AND 172.xx.xx.10 I want to change only the first IP to 172.xx.xx.2 and let the 2nd etc etc (I Have 10dns server moving and they're used on different domain and server)

I retrieve all my server export to CSV file with the following command :

  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 ";"

first file is a csv contain data like that :

"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"

The output CSV is like that :

    "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"

The output file contain the actual settings and my idea is to modify this file with the following command to use this output file to modify the settings of my server :

[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

After this modification I try to use the following command :

  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"
}

If I put manually SetDNSServerSearchOrder((“198.102.234.125",”198.102.234.126")) instead of .SetDNSServerSearchOrder(("$DNSIP")) it's working

but with $dnsip variable the result is 70 (bad IP address)

__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}

anyone can help me ? Thanks a lot

Regards Julien

Was it helpful?

Solution

According to this article you really need a string array so you should replace $DNSIP=@("$DNS") by $DNSIP.split(',').

Explanation : when you write “198.102.234.125",”198.102.234.126" you create an array, because in PowerShell , is the array operator. $DNSIP=@("$DNS") just create an array of one string which is not an IP address.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top