Building a Lab in Hyper-V with PowerShell, Part 5
March 14th, 2017 by Charlie Russel
Deploying a DHCP Server
Now that you have your forest and domain installed, including DNS, the next step to setting up a lab is the DHCP server. Start by creating a new VM for the DHCP server, trey-dhcp-03. (For details on how to create a VM with PowerShell, see Building a Lab in Hyper-V Part 2 and Part 3. ) There’s no particular need to make this a GUI installation, so build it as a Server Core installation. We’ll do the configuration all in PowerShell anyway.
Next, Install the DHCP role, add the local groups required, and authorize it in Active Directory. (Do note the slightly different server name when you go to do that, please, and you don’t need or want to promote this server to a domain controller. )
Now, as you’ll remember from earlier posts in this series, I configure all my VMs with known MAC addresses by first defining the range and then requiring a MAC address final pair parameter to New-myVM.ps1. This allows me to now configure a set of reservations for each VM in the lab, simplifying connections and making it a lot easier for me to keep track what is where.
Assuming by now you have installed the DHCP role and authorized it in Active Directory, the next step is to set up your IPv4 and IPv6 ranges. We do that by first adding a scope, then setting exclusion ranges and finally scope options. For IPv4, this is three commands:
Add-DhcpServerv4Scope -Name "Trey-Default" ` -ComputerName "trey-dhcp-03" ` -Description "Default IPv4 Scope for Lab" ` -StartRange "192.168.10.1" ` -EndRange "192.168.10.220" ` -SubNetMask "255.255.255.0" ` -State Active ` -Type DHCP ` -PassThru Add-DhcpServerv4ExclusionRange -ScopeID "192.168.10.0" ` -ComputerName "trey-dhcp-03" ` -StartRange "192.168.10.1" ` -EndRange "192.168.10.20" ` -PassThru Set-DhcpServerv4OptionValue -ScopeID 192.168.10.0 ` -ComputerName "trey-dhcp-03" ` -DnsDomain "TreyResearch.net" ` -DnsServer "192.168.10.2" ` -Router "192.168.10.1" ` -PassThru
Now, the same process for IPv6, though I usually do NOT create IPv6 reservations, but do want to set some default values.
Add-DhcpServerv6Scope -Name "Trey-IPv6-Default" ` -ComputerName "trey-dhcp-03" ` -Description "Default IPv6 Scope for Lab" ` -Prefix 2001:db8:0:10:: ` -State Active ` -PassThru Add-DhcpServerv6ExclusionRange –ComputerName trey-dhcp-03 ` -Prefix 2001:db8:0:10:: ` -StartRange 2001:db8:0:10::1 ` -EndRange 2001:db8:0:10::20 ` -PassThru Set-DhcpServerv6OptionValue -Prefix 2001:db8:0:10:: ` -ComputerName "trey-dhcp-03" ` -DnsServer 2001:db8:0:10::2 ` -DomainSearchList "TreyResearch.net" ` -PassThru
Now, create a CSV file with Names,MAC addresses(ClientID), and IPv4 Addresses. You can use your favourite plain text editor (mine is gVim), or Excel to create the CSV file. My lab has the following for the 192.168.10.xxx range of IP addresses:
Name,ClientID,IPAddress trey-edge-01,00-15-5D-32-0A-01,192.168.10.1 trey-dc-02,00-15-5D-32-0A-02,192.168.10.2 trey-dhcp-03,00-15-5D-32-0A-03,192.168.10.3 trey-dc-04,00-15-5D-32-0A-04,192.168.10.4 trey-srv-05,00-15-5D-32-0A-05,192.168.10.5 trey-wds-11,00-15-5D-32-0A-0B,192.168.10.11 Trey-Srv-12,00-15-5D-32-0A-0C,192.168.10.12 Trey-Srv-13,00-15-5D-32-0A-0D,192.168.10.13 Trey-Srv-14,00-15-5D-32-0A-0E,192.168.10.14 Trey-Srv-15,00-15-5D-32-0A-0F,192.168.10.15 Trey-Srv-16,00-15-5D-32-0A-10,192.168.10.16 Trey-client-21,00-15-5D-32-0A-15,192.168.10.21 Trey-client-22,00-15-5D-32-0A-16,192.168.10.22 Trey-client-23,00-15-5D-32-0A-17,192.168.10.23 Trey-client-24,00-15-5D-32-0A-18,192.168.10.24 Trey-client-25,00-15-5D-32-0A-19,192.168.10.25
Save the CSV file as “TreyDHCP.csv”. Now, to create the reservations, first read in the CSV file with:
$TreyDHCP = Import-CSV TreyDHCP.csv
Then, create the IPv4 reservations with a simple ForEach loop:
ForEach ($addr in $TreyDHCP ) { $ErrorActionPreference = "Continue" Add-DhcpServerv4Reservation -ScopeID 192.168.10.0 ` -Name $addr.Name ` -ClientID $addr.ClientID ` -IPAddress $addr.IPAddress ` -PassThru }
If you run multiple NICs on your lab environment, you’ll want to repeat all of the above for the second range of IP addresses.
So, here’s the whole thing in a script that supports running remotely.
<# .Synopsis Install and configure DHCP for the TreyResearch.net lab environment .Description The New-TreyDHCP script installs and configures the DHCP environment for the TreyResearch.net lab environment. It assumes a DHCP server "trey-dhcp-03" has already been created, but accepts a parameter to change the server name. The script reads a CSV file with the machine names, MAC addresses (ClientIDs), and IPv4 addresses that the that the network will use and then creates IPv4 DHCP reservations for those machines. .Example New-TreyDHCP.ps1 Reads in a list of DHCP addresses from TreyDHCP.csv and configures trey-dhcp-03 as a DHCP server with those addresses. .Example New-TreyDHCP.ps1 -ComputerName Trey-core-03 -Path c:\temp\dhcp.csv Reads in a list of DHCP addresses from c:\temp\dhcp.csv and configures the server Trey-core-03 as a DHCP server with those address reservations. .Parameter ComputerName The server to install and configure DHCP on. Default value is trey-dhcp-03 .Parameter Path The path to a CSV file with the machine names, client IDs, and IPv4 addresses to configure DHCP reservations for. The default value is .\TreyDHCP.csv. .Inputs [string] [string] .Notes Author: Charlie Russel Copyright: 2017 by Charlie Russel : Permission to use is granted but attribution is appreciated Initial: 25 March, 2014 (cpr) ModHist: 14 March, 2017 (cpr) Added ComputerName parameter and man page : #> [CmdletBinding()] Param( [Parameter(Mandatory=$False)] [alias("server")] [string] $ComputerName = 'trey-dhcp-03', [Parameter(Mandatory=$False)] [Alias("filename")] [string] $Path = '.\TreyDHCP.csv' ) if ( (Get-WindowsFeature -Name DHCP -ComputerName $ComputerName) -ne "Installed" ) { Install-WindowsFeature -Name DHCP -ComputerName $ComputerName -IncludeManagementTools } if (Test-Path $Path ) { $TreyDHCP = Import-CSV $Path } else { Throw "This script requires an input CSV file with the DHCP Reservations in it." } # Find out if the DHCP Server is already authorized. If it is, # we assume all the rest of this is done. If ( (Get-DhcpServerInDC).DnsName -match $ComputerName ) { $IsAuth = $True } else { $IsAuth = $False $DnsName = $ComputerName + ".TreyResearch.net" } # If the server isn't authorized, then nothing is set yet, so set up # our DHCP server. if (! $IsAuth) { Add-DhcpServerInDC -DnsName $DnsName -PassThru # Create local groups for DHCP # The WinNT in the following IS CASE SENSITIVE $connection = [ADSI]"WinNT://$ComputerName" $lGroup = $connection.Create("Group","DHCP Administrators") $lGroup.SetInfo() $lGroup = $connection.Create("Group","DHCP Users") $lGroup.SetInfo() Add-DhcpServerv4Scope -Name "Trey-Default" ` -Description "Default IPv4 Scope for TreyResearch Lab" ` -StartRange "192.168.10.1" ` -EndRange "192.168.10.220" ` -SubNetMask "255.255.255.0" ` -State Active ` -Type DHCP ` -ComputerName $ComputerName ` -PassThru Add-DhcpServerv4ExclusionRange -ScopeID "192.168.10.0" ` -StartRange "192.168.10.1" ` -EndRange "192.168.10.20" ` -ComputerName $ComputerName ` -PassThru Set-DhcpServerv4OptionValue -ScopeID 192.168.10.0 ` -DnsDomain "TreyResearch.net" ` -DnsServer "192.168.10.2" ` -Router "192.168.10.1" ` -ComputerName $ComputerName ` -PassThru Add-DhcpServerv6Scope -Name "Trey-IPv6-Default" ` -Description "Default IPv6 Scope for TreyResearch Lab" ` -Prefix 2001:db8:0:10:: ` -State Active ` -ComputerName $ComputerName ` -PassThru Add-DhcpServerv6ExclusionRange -Prefix 2001:db8:0:10:: ` -StartRange 2001:db8:0:10::1 ` -EndRange 2001:db8:0:10::20 ` -ComputerName $ComputerName ` -PassThru Set-DhcpServerv6OptionValue -Prefix 2001:db8:0:10:: ` -DnsServer 2001:db8:0:10::2 ` -DomainSearchList "TreyResearch.net" ` -ComputerName $ComputerName ` -PassThru } ForEach ($addr in $TreyDHCP ) { $ErrorActionPreference = "Continue" Add-DhcpServerv4Reservation -ScopeID 192.168.10.0 ` -Name $addr.Name ` -ClientID $addr.ClientID ` -IPAddress $addr.IPAddress ` -ComputerName $ComputerName ` -PassThru }
I hope you find this script useful, and I’d love to hear comments, suggestions for improvements, or bug reports as appropriate. As always, if you use this script as the basis for your own work, please respect my copyright and provide appropriate attribution.
Posted in Active Directory, Building Labs, DHCP, Hyper-V, PowerShell | Comments Off on Building a Lab in Hyper-V with PowerShell, Part 5