PowerShell: Modifying DHCP Scope Options
Recently my organization had moved toward a more Windows-based infrastructure. I needed to change the DNS server information on several of our scopes and quite frankly I’m much too lazy to do everything by hand. Enter PowerShell.
I was able to hack together a pretty quick solution to do this in bulk:
$sScopes = netsh dhcp server show scope
$sRegex = "10\.11\.\d{1,}\.\d{1,}"
$aMatches = [regex]::Matches($sScopes, $sRegex)
if ($aMatches.count -gt 0) {
foreach ($match in $aMatches) {
netsh dhcp server scope $match.Value set optionvalue 006 IPADDRESS 10.0.0.1 10.0.0.2
}
}
Let’s go through the code. The first line fetches every scope from the netsh command. The second line defines the regular expression that matches the scope addresses we want to modify. The third line actually performs the regular expression matching.
Jumping down to the loop, we run the netsh command again, but pass in the value of the match (which will be the ip address defined on line two and set our options accordingly.
I hope this helps someone out there!