Quick Tip: Manage Host NTP with PowerCLI

So along the lines of my last post, I decided I should also make another quick post about NTP on the Host side.

Check out my last post Here in case you missed it!

In order to look at the host NTP settings Select your host-Configuration-Time Configuration

From here we can see the status of the NTP Service, here called the NTP Client, and which NTP servers the Host is using.

If you want to check out the same thing but in powercli, use this one-liner!

get-vmhost|select name,@{l=”ServiceRunning”;e={get-vmhostservice $_|?{$_.key -like “ntpd”}|select -expand running }}, @{l=”NTPServer”;E={get-vmhostntpserver $_}}

It is a tad long for a one-liner, but it will display the name of the host, if the ntp service is running on the host, and the ntp servers the host is pointed to. It’s a quick way to glance at ntp host configurations in your environment.

This is all great and dandy, but if you want to change the ntp server the host points to, then I have a script for you!

function set-vmhostntp{
 param([Parameter(Mandatory=$true)]$ntp, $vmhost="*")
 #example set-vmhostntp -ntp, -vmhost myhost.vnoob.local

$vmhostinfo=get-vmhost $vmhost

$vmhostinfo |select name,@{l="ServiceRunning";e={get-vmhostservice $_|?{$_.key -like "ntpd"}|select -expand running }}, @{l="NTPServer";E={get-vmhostntpserver $_}}

remove-vmhostntpserver (get-vmhostntpserver $vmhost) -vmhost $vmhost -confirm:$false
 Add-vmhostntpserver -ntpserver $ntp -host $vmhost|out-null

$vmhostinfo|Get-vmhostservice|?{$_.key -like "ntp*"}|set-vmhostservice -policy automatic|restart-vmhostservice -confirm:$false
 $vmhostinfo|select name,@{l="ServiceRunning";e={get-vmhostservice $_|?{$_.key -like "ntpd"}|select -expand running }}, @{l="NTPServer";E={get-vmhostntpserver $_}}


So Step by Step
1. The script provides the current configuration, from my earlier one-liner.
2. It then removes all the currently configured NTP Servers on the Host
3. It adds the Ntp Servers you specify, restarting the NTP service.
4. And Finally it provides the configuration again, which should now include your specified NTP Servers

Hope this Helps!!

Leave a Reply