PowerCLI: Check NTP status on numerous ESXi hosts

I was recently tasked with configuring and checking the NTP status on a large number of ESXi hosts over a dozen different networks/vCenters. I used Host Profiles to configure NTP but wanted a way to check the time on the hosts after the profile was pushed out due to complicated network firewall rules in place.

Use connect-viserver to connect to a vCenter server before running the script. This script will prompt you to enter a filename for the CSV output. The CSV contains hostname, status of the NTP service, the startup policy for the service, a list of the NTP servers configured, and the current time of the host.

$input = Read-Host "Path for CSV Output"
$AllMembers = @()

foreach($myHost in (Get-VMHost)) {
$serviceStatus = Get-VMHostService $myHost | Where-Object {$\_.key -eq "ntpd"} | select Policy, Running

$myView = Get-View $myHost.Extensiondata.ConfigManager.dateTimeSystem

$ntpServers = Get-VMHostNtpServer $myHost

$allmembers+=new-object psobject -Property @{
Host = $myHost.Name;
StartupPolicy = $serviceStatus.Policy;
Status = $serviceStatus.Running;
ReportedTime = $myView.QueryDateTime();
NTPServers = $ntpservers.ToString();

$allmembers | Select-Object "Host", "Status", "StartupPolicy", "NTPServers", "ReportedTime" | export-csv -notypeinformation -path $input