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.