Adding stateless static routes with Get-DhcpServerv4OptionValue in PowerShell

Short little article for today. This drove me insane.

Adding a stateless static route in powershell via Get-DhcpServerv4OptionValue. Yeah you’ve searched everywhere on google, you can’t find how to make it work … well you’re in luck today my friend!

The DHCP scope you want to add these routes to need to exist first.

Let’s say you want to add the route

Subnet: 192.168.10.0 mask: 255.255.255.0 via route 192.168.10.1 to scope 10.0.0.0

The exact command will have to be

Get-DhcpServerv4OptionValue -ScopeId 10.0.0.0 -OptionId 121 -Value 24,192,168,10,192,168,10,1

Those are comma separated, yes. That’s not a mistake. Since the subnet ends with a 0 (zero), you need to NOT include that in your Value field.

Now let’s say you need to add these routes

Subnet: 192.168.10.0 mask: 255.255.255.0 via route 192.168.10.1 to scope 10.0.0.0
Subnet: 192.168.24.45 mask: 255.255.255.248 via route 192.168.10.1 to scope 10.0.0.0

Then your command will look like

Get-DhcpServerv4OptionValue -ScopeId 10.0.0.0 -OptionId 121 -Value 24,192,168,10,192,168,10,1,29,192,168,24,25,192,168,10,1

Since 255.255.255.248 in CIDR is 29, and that the subnet is not ending in a 0 (zero), you need to include those in your value. You can use the subnet calculator found here: https://www.adminsub.net/ipv4-subnet-calculator

That’s it! You’ve conquered the great DHCP mystery of adding static stateless routes! Now, go on and include that in your ansible script. I’ve shown you how in the previous article found here https://everythingvirtual.ca/creating-dhcp-scopes-on-microsoft-active-directory-with-winrm-and-ansible-from-a-ubuntu-machine/