Powershell: TimeSync in VMs and Windows w32time service settings

Customer of mine never heard about potential time sync problems in a virtual environment and when checking a number of VMs, I learned that they clearly had no default configuration for TimeSync in the VMware Tools. I decided to write a Powershell script that will list the VMware Tools setting for time sync and for Windows systems it will try to find the settings for the w32time service. The output will look like this:

Name TimeSync StartMode State OS
srv01 True Auto Running Microsoft Windows Vista (32-bit)
srv02 True Disabled Stopped Microsoft Windows XP Professional (32-bit)
srv03 False Auto Running Microsoft Windows XP Professional (32-bit)
srv04 False Unknown Unknown Red Hat Linux
srv05 True Unknown Unknown Suse Linux (32-bit)
srv06 True Disabled Stopped Microsoft Windows Server 2003, Standard Edition (32-bit)

If you are unsure on what the correct settings in your environment are, do read this KB article: “Timekeeping best practices for Windows, including NTP” and the PDF file “Timekeeping in VMware Virtual Machines“.

$view = @()
$Report = @()
$vms = Get-VM
foreach( $vm in $vms)
    {
    $row = "" | Select Name, TimeSync, StartMode, State, OS
    $view = Get-View $vm.Id
    Write-Host $vm.Name $vm.Guest.OSFullName

    # If VM Guest OS Fullname has the word "Windows" in it, check the service
    If( $vm.Guest.OSFullName -match "Windows" )
        {
        $GuestTimeService = Get-WmiObject -ComputerName $vm.Name win32_service -Filter "Name='w32time'" -ErrorVariable myErrors -ErrorAction Continue
        # Do Some error handling
        if ($myErrors.Count -gt 0)
             {
             $Err = $myErrors[0].exception
             Write-Host "Error occured: " $Err.Message
             $row.StartMode = "Error"
             $row.State = "Error"
             }
             else
             {
             $row.StartMode = $GuestTimeService.StartMode
             $row.State = $GuestTimeService.State
             }
        }
        else
        {
        $row.StartMode = "Unknown"
        $row.State = "Unknown"
        }
    $row.Name = $vm.Name
    $row.TimeSync = $view.Config.Tools.SyncTimeWithHost
    $row.OS = $vm.Guest.OSFullName
    $Report += $row
    }
$report | Export-Csv "C:\scripts\csv\TimeSync.csv"