{"id":1622,"date":"2014-04-12T00:01:00","date_gmt":"2014-04-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/04\/12\/windows-server-2012-r2-network-cmdlets-part-6\/"},"modified":"2014-04-12T00:01:00","modified_gmt":"2014-04-12T00:01:00","slug":"windows-server-2012-r2-network-cmdlets-part-6","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-6\/","title":{"rendered":"Windows Server 2012 R2 Network Cmdlets: Part 6"},"content":{"rendered":"<p><b>Summary<\/b>: Manage DHCP server settings in Windows Server 2012 R2 with Windows PowerShell.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. This weekend, I&rsquo;m getting a little geeky. I&rsquo;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:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-1\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;1<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-2\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;2<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-3\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;3<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-4\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;4<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-5\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;5<\/a><\/li>\n<\/ul>\n<p>Today I&rsquo;m going to investigate the cool new cmdlets for managing the DHCP server!<\/p>\n<p>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&rsquo;t have SCCM on the remote sites and the WAN wasn&rsquo;t passing the packets.<\/p>\n<p>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 <a href=\"http:\/\/www.energizedtech.com\/2012\/09\/powershellwake-on-lan-from-o-a.html\" target=\"_blank\">Powershell&ndash;Wake on LAN from \/\\\/\\o\\\/\\\/ and DHCP Parse from &ldquo;The Admin Guy&rdquo;&ndash;WHAT A COMBO!<\/a><\/p>\n<p>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!<\/p>\n<p>One of the challenges was to get a list of the available scopes in my original solution. In Windows Server&nbsp;2012&nbsp;R2, you can run the following cmdlet to get a list of available scopes in your DHCP server:<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4Scope<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5277.1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5277.1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>As you can see, all of the details about this scope can be easily identified&mdash;even exported for documentation!<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4Scope | Export-CSV C:\\Foo\\ContosoScopes.CSV<\/p>\n<p>But here&rsquo;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.<\/p>\n<p>First create a new scope. We&rsquo;ll pick a pretty simple configuration:<\/p>\n<p style=\"margin-left:30px\">Name &lsquo;Sample&rsquo;<\/p>\n<p style=\"margin-left:30px\">Description &lsquo;My Sample Scope Description&rsquo;<\/p>\n<p style=\"margin-left:30px\">Network 192.168.1.0<\/p>\n<p style=\"margin-left:30px\">Subnet Mask 255.255.255.0 (24 bits)<\/p>\n<p style=\"margin-left:30px\">Starting 192.168.1.100<\/p>\n<p style=\"margin-left:30px\">Ending 192.168.1.200<\/p>\n<p>We can do this in one line with Windows PowerShell:<\/p>\n<p style=\"margin-left:30px\">Add-DhcpServerv4Scope -Name &lsquo;Sample&rsquo; -StartRange 192.168.1.100 -EndRange 192.168.1.200 <br \/>&ndash;Description &lsquo;Sample for 192.168.1.0&rsquo; -SubnetMask 255.255.255.0<\/p>\n<p>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 <b>Get-DHCPServerv4OptionDefinition<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4OptionDefinition<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/83060.2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/83060.2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So I can find the names I can use for options pretty easily:<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4OptionDefinition | where { $_.Name &ndash;like &lsquo;*DNS*&rsquo; }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0726.3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0726.3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><br \/><b><\/b><\/p>\n<p>We can obtain the name we can use for the router option in the same manner:<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4OptionDefinition | where { $_.Name &ndash;like &lsquo;*Router*&rsquo; }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4760.4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4760.4.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now we can add options to our new scope by leveraging the <b>Set-DHCPServerv4OptionValue<\/b> cmdlet. But instead of using the name, we need to use the <b>OptionID<\/b> provided for the name. To add a router IP of 192.168.1.1 to our current scope called &lsquo;Sample&rsquo;, we&rsquo;d do it like this in Windows PowerShell:<\/p>\n<p style=\"margin-left:30px\">$id=(Get-DHCPServerv4Scope | Where { $_.Name &ndash;eq &lsquo;Sample&rsquo; }).ScopeID<\/p>\n<p style=\"margin-left:30px\">Set-DHCPServerv4ScopeOptionValue &ndash;Scope $Id &ndash;optionid 3 &ndash;value 192.168.1.1<\/p>\n<p>Now that we have the ID, we can set up the DNS servers for this scope. We&rsquo;ll use two internal servers 192.168.1.5 and 192.168.1.25:<\/p>\n<p style=\"margin-left:30px\">Set-DHCPServerv4ScopeOptionValue &ndash;Scope $Id &ndash;optionid 6 &ndash;value 192.168.1.5,192.168.1.25<\/p>\n<p>What&rsquo;s interesting with this cmdlet is you can&rsquo;t put in a false DHCP server. It will validate that it&rsquo;s a live server first!<\/p>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">Get-DHCPServerv4Scope | Get-DHCPServerv4Lease | Export-CSV CurrentIP.csv<\/p>\n<p>Pretty cool, eh?<\/p>\n<p>Pop back over tomorrow for our final day of Windows PowerShell Network Week, and we&rsquo;ll play with going where no cmdlet has boldly gone before: Easily managing a DNS server!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send an email to the Scripting Guys at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then just remember, the Power of Shell is in You.<\/p>\n<p><b>Sean Kearney<\/b>, Windows PowerShell MVP, Honorary Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Manage DHCP server settings in Windows Server 2012 R2 with Windows PowerShell. Honorary Scripting Guy, Sean Kearney, is here. This weekend, I&rsquo;m getting a little geeky. I&rsquo;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,37,154,61,45],"class_list":["post-1622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-networking","tag-sean-kearney","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Manage DHCP server settings in Windows Server 2012 R2 with Windows PowerShell. Honorary Scripting Guy, Sean Kearney, is here. This weekend, I&rsquo;m getting a little geeky. I&rsquo;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1622"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1622\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=1622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}