DHCP Option Values and PowerShell

Following on from my previous post, i wanted to share a small bit of PowerShell i created whilst on a site visit.

This was before i had worked on Vendor Classes but actually follows on quite nicely. In the previous post i mentioned a requirement to set an alternate NTP Server address, and used 1.uk.pool.ntp.org.

For those who may have worked with those NTP Servers in the past you may know that (as the name suggests) they are a pool of servers and the IP address returned changes frequently. So, setting that value statically and forgetting about it led me to find a new Phone on a clients desk was not syncing the time. 

A quick nslookup showed me that the value I had entered into their DHCP Server was now not returned for that DNS name. It was easy enough to update this on a one time basis, but of course we know that at some point this new value will probably no longer be valid either.

What to do?

Luckily I already knew how to resolve names with PowerShell using the [system.net.dns] .net class. I’m calling it a class but I have no idea if that is the correct term.

I knew which servers I wanted to query and entered those into an array.

$servers = @(
"0.uk.pool.ntp.org",
"1.uk.pool.ntp.org"
)

Next we can use a foreach block to query the IP addresses associated with the DNS names

foreach ($server in $servers)
{
[system.net.dns]::GetHostAddresses($server)
}

This would return a lot of information, and really we only need the IPv4 Addresses.

Lots of Information

We can use the ‘IPAddressToString’ value returned to just display the IPv4 Address.

foreach ($server in $servers)
{
$ipA = [system.net.dns]::GetHostAddresses($server)
$ipA.IpAddresstoString
}

At the time of writing this returns 8 separate IP Addresses which is probably overkill for an entry into a DHCP Scope Option.

IP Only

So lets just take the first IP returned by the DNS query.

foreach ($server in $servers)
{
$ipA = [system.net.dns]::GetHostAddresses($server)
$ipA[0].IpAddresstoString
}

This returns 1 IP for each $server we have listed.

IP0Only

Now we need to do a little more work to get those values into a format the DHCP PowerShell cmdlets will accept. In actual fact the client where i was working on this only had Server 2008 R2 so I had to format it out into NETSH!

We need to use Set-DHCPServerv4OptionValue to update our 042 value.

So we can add an empty array to store our NTP Server Values, then add each IPv4 address to that array, then run the Set-DHCPServerv4OptionValue cmdlet using our $ntpServers array. Worth noting is that the –VendorClass property is CASE SENSITIVE.

Which leaves us with the finished script.

$servers = @(
"0.uk.pool.ntp.org",
"1.uk.pool.ntp.org"
)
$ntpServers= @()
foreach ($server in $servers)
{
$ipA = [system.net.dns]::GetHostAddresses($server)
$ntpServers += $ipA[0].IpAddresstoString
}
Set-DHCPServerv4optionvalue -ScopeId 192.168.11.0 -VendorClass "Polycom-VVX400" -OptionId 042 -Value $ntpServers

We can then save this as a PS1 file and use a scheduled task to run this script and update our Scope on a regular basis.

If you’re interested in the NETSH command, it is quite similar except we push our $ntpserver array to NETSH instead of a PowerShell cmdlet.

$servers = @(
“0.uk.pool.ntp.org”,
“1.uk.pool.ntp.org”
)
$ntpServer = @()
foreach ($server in $servers)
{
$ipA = [system.net.dns]::GetHostAddresses($server)
$ntpServer += $ipa[0].IPAddressToString
}
cmd /c “netsh dhcp server 192.168.30.2 scope 172.16.0.0 set optionvalue 042 IPADDRESS vendor=Polycom-VVX400 $ntpserver”

And that, is that.

About Robert Pearman
Robert Pearman is a UK based Small Business Server enthusiast. He has been working within the SMB IT Industry for what feels like forever. Robert likes Piña colada and taking walks in the rain, on occasion he also enjoys writing about Small Business Technology like Windows Server Essentials or more recently writing PowerShell Scripts. If you're in trouble, and you can find him, maybe you can ask him a question.

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: