PowerShell: DHCP Server Scope Options Editing

Occasionally, internal DNS server changes as machines are refreshed and/or decommissioned. DHCP servers should also update according to these changes. Here is a PowerShell snippet that will ensure that this task is as painless as possible

# Set global variables
$dhcpServer1="DC01.kimconnect.com"
$dnsDomain="intranet.kimconnect.com"
$router="192.168.1.1"
$dnsClientServerIP1="10.10.10.10"
$dnsClientServerIP2="10.10.10.11"
[System.Collections.ArrayList]$dnsArray = $dnsClientServerIP1,$dnsClientServerIP2 #or convert to Array via method {$dnsArray}.Invoke()
$scopes=(Get-DhcpServerv4Scope).ScopeID.IPAddressToString

# Use this function only if all scopes are using the same DNS Client Server IP addresses
function setStandardizedDns{
foreach ($scope in $scopes){
Set-DhcpServerv4OptionValue -ComputerName $dhcpServer1 -Force -ScopeId $scope -DnsServer $dnsArray -WinsServer $dnsArray # Optional for edits: -DnsDomain $dnsDomain -Router $router
}
}

# Use this function for one-off scopes that have different primary Client DNS Server IP than the standardized $dnsArray
function setUniqueDNS($scopeID,$scopeClientPrimaryDNS){
$thisScope=$scopeID
$thisDnsArray=,$scopeClientPrimaryDNS+$dnsArray
Set-DhcpServerv4OptionValue -ComputerName $dhcpServer1 -ScopeId $thisScope -DnsServer $thisDnsArray -WinsServer $thisDnsArray -Force
}

function setServerDns{
Set-DhcpServerv4OptionValue -ComputerName $dhcpServer1 -DnsServer $dnsArray -WinsServer $dnsArray -DnsDomain $dnsDomain
}

setStandardizedDns;
setServerDns;

Leave a Reply

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