Home » Powershell » UPDATE DNS IP in List of computers

UPDATE DNS IP in List of computers

#Input file is CSV file with headers IPaddress

$Computers = import-csv “c:\temp\servers.csv”

ForEach ($Computer in $Computers) {
Write-Host “$($Computer.IPAddress): ” -ForegroundColor Yellow
Invoke-Command -ComputerName $Computer.IPAddress -ScriptBlock {

#input Cirrect DNS IP’S in the feals
$NewDnsServerSearchOrder = “10.10.1.200”,”10.10.1.106″
$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne ‘True’ -and $_.DNSServerSearchOrder -ne $null}

    # Show DNS servers before update
    Write-Host "Before: " -ForegroundColor Green
    $Adapters | ForEach-Object {$_.DNSServerSearchOrder}

    # Update DNS servers
    $Adapters | ForEach-Object {$_.SetDNSServerSearchOrder($NewDnsServerSearchOrder)} | Out-Null

    # Show DNS servers after update
    $Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne 'True' -and $_.DNSServerSearchOrder -ne $null}
    Write-Host "After: " -ForegroundColor Green
    $Adapters | ForEach-Object {$_.DNSServerSearchOrder}
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website