Introduction:
This post provides a PowerShell script that automates the configuration of DNS settings for domain controllers, including setting IP addresses, DNS server search orders, and configuring DNS forwarders and root hints.
Description:
The script iterates through a list of domain controllers, configures their network settings, and ensures that DNS forwarders and root hints are properly set up on each DNS server. This helps maintain a consistent and optimized DNS infrastructure across all domain controllers.
How to Use:
To use this script, update the placeholders with your specific network settings, domain controller information, and desired DNS forwarders. Run the script in a PowerShell session with administrative privileges on a machine that has access to the target domain controllers.
What It Does:
This script configures the network adapter settings for each domain controller, including IP address, subnet mask, default gateway, primary and secondary DNS servers. It then sets up DNS forwarders and enables or disables root hints on each DNS server.
$Forwarders = @("<put your forwarder IPs here>")
$RootHintsEnabled = $true
$DCs = @(
@{
Site = "<put your site name here>"
DCName = "<put your first DC name here>"
IPAddress = "<put your first DC IP address here>"
PrimaryDNS = "<put your first DC primary DNS here>"
SecondaryDNS = "127.0.0.1"
SubnetMask = "<put your subnet mask here>"
DefaultGateway = "<put your default gateway here>"
},
@{
Site = "<put your site name here>"
DCName = "<put your second DC name here>"
IPAddress = "<put your second DC IP address here>"
PrimaryDNS = "<put your second DC primary DNS here>"
SecondaryDNS = "127.0.0.1"
SubnetMask = "<put your subnet mask here>"
DefaultGateway = "<put your default gateway here>"
}
)
foreach ($DC in $DCs) {
Write-Host "Configuring DNS settings for $($DC.DCName)..."
$NIC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -contains $DC.IPAddress }
if ($NIC) {
$DNSSettings = $NIC.GetRelated("Win32_NetworkAdapterSetting") | Where-Object { $_.ElementName -eq "DNS Server Search Order" }
$DNSSettings.SetDNSServerSearchOrder(@($DC.PrimaryDNS, $DC.SecondaryDNS))
$IPSettings = $NIC.GetRelated("Win32_NetworkAdapterSetting") | Where-Object { $_.ElementName -eq "IP Address" }
$IPSettings.SetIPAddress(@($DC.IPAddress), @($DC.SubnetMask))
$IPSettings.SetGateways(@($DC.DefaultGateway), 1)
}
else {
Write-Host "No network adapter found with IP address $($DC.IPAddress). Skipping..."
}
}
Write-Host "DNS settings configuration complete."
$DNSServers = Get-DnsServer
foreach ($DNSServer in $DNSServers) {
Write-Host "Configuring DNS forwarders and root hints for $($DNSServer.ComputerName)..."
# Configure DNS forwarders
Set-DnsServerForwarder -ComputerName $DNSServer.ComputerName -IPAddress $Forwarders
# Enable root hints
Set-DnsServerRootHint -ComputerName $DNSServer.ComputerName -Enable $RootHintsEnabled
}
Write-Host "DNS forwarders and root hints configuration complete."
Tags: #DNSConfiguration #PowerShell #DomainControllers #NetworkSettings #ITAutomation