Change DNS IP remotely on multiple windows machines

This script doesn’t need much explanation, you need change DNS IP on multiple machnes (Usually Servers, as clients get DNS IP from DHCP) and you need to do it fast. This is how you do it.

$computer = get-content .\servers.txt

foreach ($comp in $computer){
    try{
        $a = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $comp -ErrorAction Stop | Where-Object {$_.ipenabled -eq "true"}).InterfaceIndex
        Invoke-Command -ComputerName $comp -ScriptBlock {param( $b) netsh interface ip add dns name=$b ip1 } -args $a -ErrorAction Stop
        Invoke-Command -ComputerName $comp -ScriptBlock { param( $b) netsh interface ip add dns name=$b ip2 index=2} -args $a -ErrorAction Stop
        }
    catch
    {
    Write-Output "Failed  $comp"  | Out-File .\error.txt -Append
    }
}

Needles to mention, DNS is just an example, you could use it to modify other interface properties as well.

Be the first to comment

Leave a Reply

Your email address will not be published.


*