I am working to retire a DNS server and would like to remove the address from any servers with it currently listed. Everything seems to run correctly with the exception of the actual application of the new DNSServerSearchOrder. I get a ReturnValue of 70 which refers to an invalid address so I assume the input going into the “SetDNSServerSearchOrder()” command is in the wrong format. Can anyone tell me how to appropriately format the $newDNS variable to successfully plug into “SetDNSServerSearchOrder” command?
[hr]
[i]
$targets = Get-Content “E:\Powershell\Input\ServersTest.txt”[/i]
$removeAddress = “1.1.1.3”
$newDNS = $null
$address = $null
$filePath = “E:\Powershell\Output\DNSChangeLog.txt”
$time = (Get-Date -Format “h:mm tt”).ToString()
$date = (get-date).ToString(‘MM/dd/yyyy’)
$removeAddress2 = “" + $removeAddress + "”New-Item -Path $filePath -ItemType File -Force
Add-Content -Path $filePath -Value ($time + " on " + $date)ForEach($target in $targets) {
$NICs = Get-WmiObject Win32_NetworkAdapterConfiguration -Computername $target | Where{$.IPEnabled -eq “TRUE” -and $.DHCPEnabled -eq $false}
ForEach($NIC in $NICs) {
$DNS = $NIC.DNSServerSearchOrder
If($DNS -like $removeAddress2) {
$newDNS = $DNS -replace $removeAddress
$NIC.SetDNSServerSearchOrder($newDNS)
$newDNS = $null
}
Else {
$text = "No DNS changes made to " + $target + " for adapter " + $NIC.ServiceName + “`n”
Add-Content -Path $filePath -Value $text
}} "-------------------"}