Using Vagrant to Deploy a 2012R2 DHCP Failover Cluster

Share on:

So I am in the process of rolling out a new 2012R2 DHCP Server Cluster, and I thought i would share my scripts with the community.

I have also rolled it into a Vagrant package so you can easily test it on your own, and execute the scripts by themselves against your environment to deploy.

Here is a link to the GitHub repository containing all the code.

The code is also fairly simple, it pulls in data from a few .CSV files and deploy’s the servers, installs the roles, creates the scopes, creates MAC Address allow policies and then creates a couple reservations.

Files to modify to customize your installation

  • dhcpservers.csv - IP’s of your two DHCP servers
  • createscopes.csv - Contains your Scope Name, StartRange, EndRange, Mask and Server to deploy to
  • scopeoptions.csv - Contains your server specific options. You can add more columns and adjust the code to add alternate options
  • scopeoptions.csv - Contains your scope specific options. You can add more columns and adjust the code to add alternate options
  • scopefailover.csv - Used to create Failover Scopes, Specify the ScopeID, PartnerServer, LoadBalancePercent, MaxClientLeadTime, AutoStateTransition, StateSwitchInterval and Server to deploy against
  • scopepolicy.csv - Used to create Scope Policies. In this example it creates a MAC Allow list, so only Mac Addresses listed will be allowed to pull leases.
  • scopereservations.csv - Used to create Scope Reservations. Input your ScopeID, MAC Address and Reservation Name. It automatically pulls an available IP and reserves it.

Execute the Vagrant package by downloading it and running.

1vagrant up

Execute the powershell script by downloading the scripts folder and running.

1.\install_ha_dhcp.ps1

Here is a copy of the raw code.

 1# Powershell Script for Vagrant to install and configure DHCP
 2# @davidstamen
 3# http://davidstamen.com
 4
 5cd c:\vagrant\scripts
 6
 7$username = "vagrant"
 8$password = "vagrant"
 9$secstr = New-Object -TypeName System.Security.SecureString
10$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
11$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
12
13# Install the DHCP Role
14Write-Host "Installing DHCP Server Role" -ForegroundColor "Red"
15$csv = Import-CSV .\dhcpservers.csv
16foreach ($server in $csv) {
17  Install-WindowsFeature DHCP -IncludeAllSubFeature -IncludeManagementTools -Computer $server.IP -Credential $cred -Verbose
18}
19
20# Authorize the DHCP server in Active Directory
21#Write-Host "Adding DHCP Server to AD" -ForegroundColor "Red"
22#$csv = Import-CSV .\dhcpservers.csv
23#foreach ($server in $csv) {
24#Add-DhcpServerInDC -DnsName $server.Name -IPAddress $server.IP
25#}
26
27# Create an IPv4 DHCP scope
28Write-Host "Adding DHCP Scopes" -ForegroundColor "Red"
29$csv = Import-CSV .\createscopes.csv
30foreach ($scope in $csv) {
31    Add-DhcpServerv4Scope -ComputerName $scope.Server -Name $scope.Name -StartRange $scope.Start -EndRange $scope.End -SubnetMask $scope.mask -Verbose
32}
33
34# Set DHCP Server Options
35Write-Host "Setting DHCP Server Options" -ForegroundColor "Red"
36$csv = Import-CSV .\serveroptions.csv
37foreach ($scope in $csv) {
38  Set-DhcpServerv4OptionValue -ComputerName $scope.Server -DnsDomain $scope.DNSDomain -DNSServer $scope.DNSServer -Verbose
39}
40
41# Set DHCP Scope Options
42Write-Host "Setting DHCP Scope Options" -ForegroundColor "Red"
43$csv = Import-CSV .\scopeoptions.csv
44foreach ($scope in $csv) {
45  Set-DhcpServerv4OptionValue -ComputerName $scope.Server -ScopeId $scope.scopeID -Router $scope.Router -Verbose
46  Set-DhcpServerv4Scope -ComputerName $scope.Server -ScopeId $scope.scopeID -LeaseDuration $scope.Lease -Verbose
47}
48
49# Configure Failover
50Write-Host "Configuring DHCP Failover" -ForegroundColor "Red"
51$csv = Import-CSV .\scopefailover.csv
52foreach ($scope in $csv) {
53  Add-DhcpServerv4Failover -ComputerName $scope.Server -Name $scope.scopeID -PartnerServer $scope.PartnerServer -ScopeId $scope.scopeID -LoadBalancePercent $scope.LoadBalancePercent -MaxClientLeadTime $scope.MaxClientLeadTime -AutoStateTransition ([System.Convert]::ToBoolean($scope.AutoStateTransition)) -StateSwitchInterval $scope.StateSwitchInterval -Verbose
54}
55
56#Create MAC Allow List DHCP policy
57Write-Host "Creating MAC Allow List Policy" -ForegroundColor "Red"
58$csv = Import-CSV .\scopepolicy.csv
59foreach ($scope in $csv) {
60  $maclist = @()
61  $operator = $scope.Operator
62  $maclist+=$operator
63  $maclist+=$Scope.MAC1
64  $maclist+=$Scope.MAC2
65  Add-DhcpServerv4Policy -ComputerName $scope.Server -Name $scope.Name -Description $scope.Description -ScopeId $scope.ScopeID -Condition $scope.Condition -MacAddress $maclist -Verbose
66  Add-DhcpServerv4PolicyIPRange -ComputerName $scope.Server -Name $scope.Name -ScopeId $scope.ScopeID -StartRange (Get-DHCPServerv4Scope $scope.ScopeID).StartRange -EndRange (Get-DHCPServerv4Scope $scope.ScopeID).EndRange -Verbose
67}
68
69#Reserve IP's
70Write-Host "Reserving DHCP IPs" -ForegroundColor "Red"
71$csv = Import-CSV .\scopereservations.csv
72foreach ($scope in $csv) {
73  Add-DhcpServerv4Reservation -ComputerName $scope.Server -ScopeId $scope.ScopeID -IPAddress (Get-DhcpServerv4FreeIPAddress -ComputerName $scope.Server -ScopeId $scope.ScopeID) -ClientId $Scope.MAC -Name $scope.Name -Verbose
74}
75
76#Replicate Settings
77Write-Host "Forcing Replication" -ForegroundColor "Red"
78Get-DhcpServerv4Failover -ComputerName $scope.Server|Invoke-DhcpServerv4FailoverReplication -ComputerName $scope.Server -Force
79#Convert Leases to Reservation
80#Get-DhcpServerv4Lease -ComputerName $server1 -ScopeID 10.10.10.0 | Add-DhcpServerv4Reservation -ComputerName $server1
81
comments powered by Disqus

See Also