- Windows Server 2012 Automation with PowerShell Cookbook
- Ed Goad
- 392字
- 2025-02-24 16:00:15
Configuring DHCP scopes
As an alternative to statically assigned TCP/IP addresses, Windows supports the Dynamic Host Configuration Protocol (DHCP). This service allows for provisioning of IP addresses, default gateways, DNS information, and even more advanced information such as boot servers.
This recipe will set up the basic DHCP features on a domain controller and configure an initial DHCP scope.
Getting ready
This recipe assumes a server, networking, and domain configuration similar to what is created in the Installing domain controllers recipe.
How to do it...
Carry out the following steps to configure DHCP scopes:
- Install DHCP and management tools:
Get-WindowsFeature | Where-Object Name -like *dhcp* Install-WindowsFeature DHCP -IncludeManagementTools
- Create a DHCP scope
Add-DhcpServerv4Scope -Name "Corpnet" -StartRange 10.10.10.100 -EndRange 10.10.10.200 -SubnetMask 255.255.255.0
- Set DHCP options
Set-DhcpServerv4OptionValue -DnsDomain corp.contoso.com -DnsServer 10.10.10.10 -Router 10.10.10.1
- Activate DHCP
Add-DhcpServerInDC -DnsName corpdc1.corp.contoso.com
How it works...
The first step uses Install-WindowsFeature
to install the DHCP feature and management tools on the currently logged on system. Once installed, the second step calls Add-DHCPServerv4Scope
to create a DHCP scope named Corpnet
, providing dynamic IPs on the 10.10.10.0/24 subnet
.
The third step uses Set-DhcpServerv4OptionValue
to set up common DHCP options, such as the DNS servers and default gateway address. This command can include other common options such as the DNS domain name, WinsServer, and Wpad location. Additionally, any extended DHCP option ID can be configured using the Set-DhcpServerv4OptionValue
command.
The last step calls Add-DHCPServerInDC
to activate the DHCP service on the computer in Active Directory. This authorizes the DHCP service to provide addresses to clients in the domain.
There's more...
The following lists the additional features of DHCP:
- Adding DHCP reservations: In addition to creating and activating DHCP scopes, we can also create reservations in DHCP. A reservation matches a network adapter's MAC address to a specific IP address. It is similar to using a static address, except the static mapping is maintained on the DHCP server:
Add-dhcpserverv4reservation –scopeid 10.10.10.0 –ipaddress 10.10.10.102 –name test2 –description "Test server" –clientid 12-34-56-78-90-12 Get-dhcpserverv4reservation –scopeid 10.10.10.0
- Adding DHCP exclusions: Additionally, we can create DHCP exclusions using PowerShell. An exclusion is an address, or range of addresses that the DHCP server won't provide to clients. Exclusions are often used when individual IP addresses within the scope have been statically assigned:
Add-DhcpServerv4ExclusionRange –ScopeId 10.10.10.0 –StartRange 10.10.10.110 –EndRange 10.10.10.111 Get-DhcpServerv4ExclusionRange