Summary: Manage DHCP server settings in Windows Server 2012 R2 with Windows PowerShell.
Honorary Scripting Guy, Sean Kearney, is here. This weekend, I’m getting a little geeky. I’m going to continue with more of Windows PowerShell and the network cmdlets. This is the sixth part in a series called Windows PowerShell Network Week. You also might enjoy reading:
- Windows Server 2012 R2 Network Cmdlets: Part 1
- Windows Server 2012 R2 Network Cmdlets: Part 2
- Windows Server 2012 R2 Network Cmdlets: Part 3
- Windows Server 2012 R2 Network Cmdlets: Part 4
- Windows Server 2012 R2 Network Cmdlets: Part 5
Today I’m going to investigate the cool new cmdlets for managing the DHCP server!
Oh I wish I had these at my last job! We were deploying new workstations and imaging systems. For this to work, I needed to send a Wake on Lan to the workstations. The problem was that I didn’t have SCCM on the remote sites and the WAN wasn’t passing the packets.
So I figured I could use Windows PowerShell and a cool Wake on Lan script from Marcus Van Orsow (/\/\0\/\/) running on the remote servers and my local one to do the trick. I combined this with a DHCP parsing trick from The Admin Guy. For the original post of this solution, see Powershell–Wake on LAN from /\/\o\/\/ and DHCP Parse from “The Admin Guy”–WHAT A COMBO!
I was happy with the solution, but at the time, I really wished that there was an easier way to work with DHCP. Now there is!
One of the challenges was to get a list of the available scopes in my original solution. In Windows Server 2012 R2, you can run the following cmdlet to get a list of available scopes in your DHCP server:
Get-DHCPServerv4Scope
As you can see, all of the details about this scope can be easily identified—even exported for documentation!
Get-DHCPServerv4Scope | Export-CSV C:\Foo\ContosoScopes.CSV
But here’s where Windows PowerShell rocks even more. What I can do now for modern day servers is use the cmdlets to build out a DHCP server.
First create a new scope. We’ll pick a pretty simple configuration:
Name ‘Sample’
Description ‘My Sample Scope Description’
Network 192.168.1.0
Subnet Mask 255.255.255.0 (24 bits)
Starting 192.168.1.100
Ending 192.168.1.200
We can do this in one line with Windows PowerShell:
Add-DhcpServerv4Scope -Name ‘Sample’ -StartRange 192.168.1.100 -EndRange 192.168.1.200
–Description ‘Sample for 192.168.1.0’ -SubnetMask 255.255.255.0
You can also edit the options for your scope with the same cmdlets. To make things easier, you can get a list of the available options defined in your DHCP server by using the Get-DHCPServerv4OptionDefinition cmdlet:
Get-DHCPServerv4OptionDefinition
So I can find the names I can use for options pretty easily:
Get-DHCPServerv4OptionDefinition | where { $_.Name –like ‘*DNS*’ }
We can obtain the name we can use for the router option in the same manner:
Get-DHCPServerv4OptionDefinition | where { $_.Name –like ‘*Router*’ }
Now we can add options to our new scope by leveraging the Set-DHCPServerv4OptionValue cmdlet. But instead of using the name, we need to use the OptionID provided for the name. To add a router IP of 192.168.1.1 to our current scope called ‘Sample’, we’d do it like this in Windows PowerShell:
$id=(Get-DHCPServerv4Scope | Where { $_.Name –eq ‘Sample’ }).ScopeID
Set-DHCPServerv4ScopeOptionValue –Scope $Id –optionid 3 –value 192.168.1.1
Now that we have the ID, we can set up the DNS servers for this scope. We’ll use two internal servers 192.168.1.5 and 192.168.1.25:
Set-DHCPServerv4ScopeOptionValue –Scope $Id –optionid 6 –value 192.168.1.5,192.168.1.25
What’s interesting with this cmdlet is you can’t put in a false DHCP server. It will validate that it’s a live server first!
Now thinking back to my original problem with DHCP servers, I could have easily pulled out the current list of leased IP addresses from all of my scopes by doing something as simple as this and storing them into a CSV file:
Get-DHCPServerv4Scope | Get-DHCPServerv4Lease | Export-CSV CurrentIP.csv
Pretty cool, eh?
Pop back over tomorrow for our final day of Windows PowerShell Network Week, and we’ll play with going where no cmdlet has boldly gone before: Easily managing a DNS server!
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to the Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then just remember, the Power of Shell is in You.
Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy