Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use functions from the DHCPServer module to create multiple IPv4 scopes.
Microsoft Scripting Guy, Ed Wilson, is here. One thing that is great about Windows PowerShell is that, in general, it always behaves in a similar fashion. This means that techniques I learned when working with Active Directory should also carry over when I am working with Microsoft Exchange Server. Of course, there are some idiosyncrasies, but in general, PowerShell is PowerShell is PowerShell. This is why it is important to learn the fundamentals of working with Windows PowerShell. Do not expect a Windows PowerShell book for every product known to Microsoft—because it will not happen. Learn Windows PowerShell, and you will be able to administer any product you need to use. Great places to start are the Scripting Wife Blog posts, as well as the Learn PowerShell page on the Script Center.
Note This is the sixth article in a series of Hey, Scripting Guy! Blog posts where I talk about using Windows PowerShell to work with Microsoft DHCP servers. I began the series with Use PowerShell to Query AD DS for DHCP Servers, in which I talked about using the Get-ADObject cmdlet to query for DHCP servers. I followed this with Use PowerShell Functions to Authorize DHCP Servers. In this blog post, I talked about using several Windows PowerShell functions from the DHCPServer module. I query for DHCP servers, authorize DHCP servers, and deauthorize DHCP servers in this post. Next, I wrote Weekend Scripter: DHCP Address Conflict Detection in which I talked about making changes to the DHCP server configuration. Then, I wrote Weekend Scripter: Parsing the DHCP Database? No Way! Yesterday, I wrote about creating a DHCP scope via PowerShell. Today’s post builds on these articles, and I recommend you read them in order as I am not level-setting in each article.
Normally, one would think that to create multiple DHCP server scopes would require a lot of scripting. It might be complicated, but the time saved, over manual configuration, would be worth it. That might have been true in the old days, but with Windows PowerShell that is not true. In fact, I used a one-line logical command to create a half dozen DHCP server scopes. It took me less than five minutes to do this, and it worked perfectly the first time.
The trick, if it can be called a trick, is to create a text (CSV) file that contains the information to create each of the DHCP scopes. The important thing is that each of the column headings needs to be the same as the function parameter names. The image that follows illustrates the CSV file I created.
To read the CSV file, use the Import-CSV cmdlet. The nice thing about this cmdlet is that it creates property/value pairs from the file thereby creating a series of objects. Windows PowerShell loves objects, so whenever the opportunity presents itself, I always like to use an object rather than attempting to parse strings. The command to import the CSV file is shown here.
Luckily, most of the Add-DhcpServerv4Scope parameters accept piped input. I used the –full switch with the Get-Help cmdlet to examine the parameters. For example, the Help for the –StartRange parameter shows that it accepts piped input by PropertyName.
Specifies the starting IP address of the range within the subnet from which
IP addresses should be leased by the DHCP server service.
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? False
One thing that is interesting is that the -ComputerName parameter does not accept piped input. Therefore, even though I added a CN (parameter alias) column heading, I ended up not using it.
After importing the CSV file, pipe the resultant objects to the Add-DHCPServerv4Scope function. Because the ComputerName parameter does not accept piped input, I have to hard code the ComputerName parameter, as shown here.
import-csv C:\fso\DHCPSubnets.txt | Add-DhcpServerv4Scope -cn wds1
No output comes from running this command. I have two choices when it comes to verifying the command. The first is to use Windows PowerShell as indicated here.
18:18 C:\> Get-DhcpServerv4Scope -cn wds1 | ? description -match 'test'
ScopeId SubnetMask Name State StartRange EndRange
------- ---------- ---- ----- ---------- --------
192.168.6.0 255.255.255.0 scope1 Active 192.168.6.1 192.168.6...
192.168.7.0 255.255.255.0 scope2 Active 192.168.7.1 192.168.7...
192.168.8.0 255.255.255.0 scope3 Active 192.168.8.1 192.168.8...
192.168.9.0 255.255.255.0 scope4 Active 192.168.9.1 192.168.9...
192.168.10.0 255.255.255.0 scope5 Active 192.168.10.1 192.168.1...
192.168.11.0 255.255.255.0 scope6 Active 192.168.11.1 192.168.1...
Of course, the other thing I can do is use the DHCP tool. This is shown here.
One thing, from a test perspective, is that when I use Windows PowerShell to create something, I also want to know how to use Windows PowerShell to remove or delete the same something. This is important for people who do DEMOs and need to know how to reset their test environment. It is also important when running a complex script that needs running and tweaking for quite some time.
Well, the good news is that to clean up after running the command for creating a bunch of DHCP scopes requires a very simple one-liner. Here it is.
Get-DhcpServerv4Scope -cn wds1 | ? description -match 'test' | Remove-DhcpServerv4Scope -cn wds1
Join me tomorrow when we have the fourth article in the Windows PowerShell Workflow series by Microsoft Windows PowerShell MVP Richard Siddaway. It is awesome, so don’t miss out.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Hi, this is awesome! I have to create DHCP scopes for 50 new VLANs in our infrastructure, and this helped A LOT! By the way, how is domain name, Gateway and DNS added to the script? Thanks! :-)
How would you add gateway, DNS, ETC?
Thanks for the great post!