That… Could Be A Problem…

28Oct/130

PowerCLI – One-Liner – Check All VMHosts’ Time

It's everyone's favorite time of year, daylight savings time. If your NTP solution is solid, there's no worries and you've never had to think twice about it. If you're like everyone else, you at least want to run a couple scripts to verify and validate everything is looking good.

A little bit of background, the environment I was running this report on had 100+ hosts.

An easy one-liner:
Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}

Depending on the size of your environment, this could take 20 seconds or an hour. In my case, it took 14 minutes. However, due to the time it took to run, quite a few hosts were reporting different times. A discrepancy of 14 minutes either way meant that the hosts were all just about in sync. If you're only worried about time zone issues, a 14 minute discrepancy is acceptable.

Example based upon the environment I was working in:
PowerCLI C:\ > Get-Date -Format g; Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}; Get-Date -Format g

10/9/2013 10:19 AM
Name Current VMHost Time
---- -------------------

10/9/2013 10:33 AM

At this point you may be thinking, there has to be a faster way to do this. You're right, there is. To do so, you have to leave the comfort of the PowerCLI cmdlets behind and make a swap over to the .NET view objects cmdlets. In this case, Get-View will be used.

Good news, it is still another one-liner:
Get-View -ViewType HostSystem -Property Name,ConfigManager.DateTimeSystem | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ConfigManager.DateTimeSystem).QueryDateTime()}}

As you can see, there's a bit of a difference between the one-liners and there will be a bit of a learning curve as well. The time savings will be worth it though. To prove it, this version only took 2 minutes to run. That's a 12 minute savings!

Example based upon the environment I was working in:
PowerCLI C:\ > Get-Date -Format g; Get-View -ViewType HostSystem -Property Name,ConfigManager.DateTimeSystem | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ConfigManager.DateTimeSystem).QueryDateTime()}}; Get-Date -Format g

10/9/2013 10:35 AM

Name Current VMHost Time
---- -------------------

10/9/2013 10:37 AM

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.