Quantcast
Channel: Windows – rakhesh.com
Viewing all articles
Browse latest Browse all 163

Adding DHCP scope options via PowerShell

$
0
0

Our deployment team needed a few DHCP options set for all our scopes. There was a brickload of these scopes, no way I was going to go to each one of them and right-click add the options! I figured this was one for PowerShell!

Yes, I ended up taking longer with PowerShell coz I didn’t know the DHCP cmdlets but hey (1) now I know! and (2) next time I got to do this I can get it done way faster. And once I get this blog post written I can refer back to it that time.

The team wanted four options set:

  • Predefined Option 43 – 010400000000FF
  • Custom Option 60 – String – PXEClient
  • Predefined Option 66 – 10.x.x.x
  • Predefined Option 67 – boot\x86\wdsnbp.com

PowerShell 4 (included in Windows 8.1 and Server 2012 R2) has a DHCP module providing a bunch of DHCP cmdlets.

First things first – I needed to filter out the scopes I had to target. Get-DhcpServerv4Scope is your friend for that. It returns an array of scope objects – filter these the usual way. For example:

Get-DhcpServerv4Scope -ComputerName MyDHCPServer | ?{ $_.Name -notmatch "Voice" -and $_.Name -notmatch "Wireless" }

Now, notice that one of the options to be added is a custom one. Meaning it doesn’t exist by default. Via GUI you would add it by right clicking on “IPv4″ and selecting “Set Predefined Options” then adding the option definition. But I am doing the whole thing via PowerShell so here’s what I did:

Add-DhcpServerv4OptionDefinition -ComputerName MyDHCPServer -Name PXEClient -Description "PXE Support" -OptionId 060 -Type String

To add an option the Set-DhcpServerv4OptionValue is your friend. For example:

Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId "MyScope" -OptionId 060 -Value "PXEClient"

I had a bit of trouble with option 43 because it has a vendor defined format and I couldn’t input the value as given. From the help pages though I learnt that I have to give it in chunks of hex. Like thus:

Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId "MyScope" -OptionId 043 -Value 0x01,0x04,0x00,0x00,0x00,0x00,0xFF

Wrapping it all up, here’s what I did (once I added the new definition):

Get-DhcpServerv4Scope -ComputerName MyDHCPServer | ?{ $_.Name -notmatch "Voice" -and $_.Name -notmatch "Wireless" } | %{ $Scope = $_.ScopeId;
	Write-Host -ForegroundColor Yellow "Working on $Scope" 
	Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId $Scope -OptionId 043 -Value 0x01,0x04,0x00,0x00,0x00,0x00,0xFF
	Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId $Scope -OptionId 060 -Value "PXEClient"
	Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId $Scope -OptionId 066 -Value "10.x.x.x"
	Set-DhcpServerv4OptionValue -ComputerName MyDHCPServer -ScopeId $Scope -OptionId 067 -Value "boot\x86\wdsnbp.com"
}

And that’s it!


Viewing all articles
Browse latest Browse all 163

Trending Articles