In Windows Server 2012 you can employ PowerShell to configure DHCP. While there are dozens of PowerShell DHCP functions (cmdlets), on this page I concentrate on the aptly named DhcpServerv4Scope.
- Create a DHCP Scope with PowerShell
- Research PowerShell DHCP Functions (Cmdlets)
- Edit Your DHCP Scope with Set-DhcpServerv4Scope
- Remove a DHCP Scope with PowerShell
Basics of Creating a DHCP Scope
When you assign IPv4 and IPv6 addresses via a DHCP server the key concept is the scope. The minimum four properties to specify are: a name for the scope, the starting and ending IP address of the range, and the subnet mask. For the sake of getting started I will gloss over the numerous optional properties.
# PowerShell 3.0 creates a DHCP Scope
Check your new scope
I recommend that you compare:
a) The results of PowerShell's:
b) What you see in the DHCP Server's GUI, see screenshot above right.
Windows Server 12 DHCP Server's GUI
SolarWinds have produced three Active Directory add-ons. These free utilities have been approved by Microsoft, and will help to manage your domain by:
- Seeking and zapping unwanted user accounts.
- Finding inactive computers.
- Bulk-importing new users. Give this AD utility a try, it’s free!
I want to concentrate on the DHCPServerV4 commands, note that 'V', and be aware of the parallel IP V6 commands.
# Research PowerShell's DHCP Cmdlets
Resulting Functions (Cmdlets):
It's taking a while to get used to the concept that Add-DhcpServerv4Scope, and similar Windows Server 12 PowerShell commands, are not actually cmdlets, but functions.
Challenge 1: Repeat, but substitute 'Get' for 'Add'.
Challenge 2: Go the whole nine yards, substitute '*' for 'Add'.
Configuring DHCP through PowerShell looks easy; after all there are dozens of cmdlets, or to give them their proper term -functions. However, the secret of success is to pay close attention to the ScopeId property. In this example its value is precisely: 192.168.1.0
# PowerShell sets DHCP lease to 9 days.
Set-DhcpServerv4Scope -ScopeId 192.168.1.0 -LeaseDuration (New-TimeSpan -Days 9)
Note 2: -ScopeId took me ages to figure out. My problem was overthink, no need for any brackets or speech marks, just the plain IP address. Remember that zero at the end and forget about appending the name of the scope.
Note 3: The syntax of -LeaseDuration, or -Lease if you prefer, cause me a few headaches. Just study my timespan example and amend Days 9 as needed.
Research DHCP Properties or Parameters
Let us assume that you need to add information to the DHCP scope, for example, to set a description, or to change the 'State' from inactive to active. To research parameter I recommend that you prefix the PowerShell DHCP function with Get-Help.
# Research PowerShell's DHCP parameters
Note 4: This reveals new parameters such as -State and -Description. It also reminds us of old friends such as -LeaseDuration and -EndRange.
Note 5: You see pretty much the same parameters if substitute the verbs, Get or Remove for Set.
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
Perhaps the NPM’s best feature is the way it suggests solutions to network problems. Its second best feature is the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you give this Network Performance Monitor a try.
My use for this technique was in testing. In truth I got in a right old stew before I discovered the simple and elegant:
Remove-DhcpServerv4Scope -ScopeId 192.168.1.0
Note 6: For consistency PowerShell always uses the Remove verb, and never delete or kill.
The following shows the thought processes of a demented PowerShell scripter.
$Scopy = (Get-DhcpServerv4Scope -ScopeId 192.168.1.0).ScopeId.IPAddressToString
Remove-DhcpServerv4Scope -ScopeId $Scopy
Note 7: This example proved to me that the value of the ScopeId was a pure dot decimal number 192.168.1.0, and not [192.168.1.0 Guy] or other even more bizarre ideas.
Summary of Add-DhcpServerv4Scope in Windows Server 2012
How PowerShell's DHCP scope functions (cmdlets) can configure Windows Server 2012. Examples of Add and Set-DhcpServerv4Scope explain how to create the scope's range and set the lease.
If you like this page then please share it with your friends