Script changing the DNS resolvers


The following is a method to automate changing the DNS resolvers on manually assigned devices like servers.

Note: Test on small sample, do not execute on production environment as more tests might be required

1. Create a TXT file under the name of servers, and add the hostnames like below:
DP-CLIENT-02.EIP.LOCAL

DP-CLIENT-04.EIP.LOCAL

AD.EIP.LOCAL

2. Create a PowerShell script to read the servers.txt file, check the active adapters, and change the DNS resolvers to the EIP IPs:
Save the script .ps1 extension:

# Path to the servers file

$serversFilePath = "C:\servers.txt"

# Path to the log file

$logFilePath = "C:\dns_change_log.txt"

 

# Function to append log to the file

function Write-Log {

    Param ([string]$message)

    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

    "$timestamp - $message" | Out-File -FilePath $logFilePath -Append

}

 

# Read hostnames from the file

$hostnames = Get-Content -Path $serversFilePath

 

foreach ($hostname in $hostnames) {

    # Use Invoke-Command for PowerShell Remoting

    Invoke-Command -ComputerName $hostname -ScriptBlock {

        $adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }

        if ($adapters.Count -eq 0) {

            Write-Output "No active network adapters found."

            return

        }

 

        foreach ($adapter in $adapters) {

            try {

                Set-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex -ServerAddresses ("10.5.10.42", "10.5.10.45")

                Write-Output "DNS settings updated for adapter: $($adapter.Name)"

            } catch {

                Write-Output "Failed to update DNS settings for adapter: $($adapter.Name). Error: $_"

            }

        }

    }

}

3. Run the ps1 file using PowerShell with domain admin privileges. 


Did you find this article useful?



  • Measure QPS on BIND9

    BIND9 DNS engine was installed on Ubuntu server, the goal is to measure the QPS by enabling the statistics.Installing the BIND9 command: sudo apt inst...

  • Enabling DNSSEC on an external zone

    Domain Name System Security Extensions (DNSSEC) is used to strengthen DNS protocol security.It controls the integrity of all DNS answers and ensures t...

  • Utilizing ioc2rpz.net as open source RPZ

    ioc2rpz community is a portal which provides open source DNS Firewall / RPZ feeds. The DNS Firewall feeds are based on publicly available threat intel...

  • Enable Guardian for nonsupported interfaces

    Broadcom interface is not supported by default, only intel interfaces are supported for the Guardian service. this workaround only for POC: 1. login...

  • Cascaded DNS

    EDNS: Port 53 is reserved for DNS usage, DNS uses both UDP and TCP for message transport.Conventional message exchanges are short, and thus well suit...