scope

Use PowerShell to Recreate DHCP Scopes

Q: Is there an easy way to create a new DHCP scope and options through PowerShell?

A: I recently had to delete a bunch of DHCP scopes, then recreate them and enable failover replication. Because I wanted to do them one at a time, I created the following basic PowerShell script.

$3rdOctet = 5

Remove-DhcpServerv4Scope -ScopeId 10.0.$3rdOctet.0 -Force

Add-DhcpServerv4Scope -EndRange 10.0.$3rdOctet.200 -Name Scope-$3rdOctet -StartRange 10.0.$3rdOctet.50 -SubnetMask 255.255.255.0 -LeaseDuration 8.0:0:0 -State Active -Type Dhcp
Set-DhcpServerv4OptionValue -ScopeId 10.0.$3rdOctet.0 -OptionId 3 -Value 10.0.$3rdOctet.1
Set-DhcpServerv4OptionValue -ScopeId 10.0.$3rdOctet.0 -OptionId 6 -Value 10.0.$3rdOctet.10,10.1.1.10 -Force

Add-DhcpServerv4Failover -ScopeId 10.0.$3rdOctet.0 -PartnerServer DHCP02 -Name TVLAN-$3rdOctet
Get-DhcpServerv4Failover -ScopeId 10.0.$3rdOctet.0

Note that in my environment all the scopes are 10.0.<ID>.0 (e.g., 10.0.1.0, 10.0.2.0, and so on). They also use one DHCP server from the scope, 10.0.<ID>.10, and a general DNS server, 10.1.1.10. Addresses 50 through 200 are used for the actual scope. You might want to modify the actual values for your environment. Just change the 3rdOctet variable at the start for the specific scope you want to delete, create, then enable for replication.

The replica server in my environment is DHCP02; you'd need to change it to the name of your partner DHCP server. My script doesn't capture any current leases; therefore, you should ensure that IPv4 is configured to test for an actual IP use of at least one try before allocating. You can configure this on the IPv4 properties Advanced tab as Conflict detection attempts. This means that the server will try to contact an IP address before allocating it. If there's no response, the server will go ahead and lease the IP address.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish