I found a couple of cases where $dts=get-view$_.ConfigManager.DateTimeSystem needs to be refined. It can occasionally return more than 1 value.
For these cases I modified the line after that to read:
$t=$dts[0].QueryDateTime()
This makes sure that the subtraction operation only has a single value to work on.
Here is the full copy of the version I'm using:
Function CheckHostDateTime ( $testHost ) {
#get host datetime system
$dts = get-view $testHost.ExtensionData.configManager.DateTimeSystem
#get host time (sometimes DateTimeSystem returns more than 1 value. Only use the 1st one)
$t = $dts[0].QueryDateTime()
Write-Host "Time on " $testHost $t "(UTC)"
#calculate time difference in seconds
Try { $s = ( $t - [DateTime]::UtcNow).TotalSeconds
#check if time difference is too much
if([math]::abs($s) -gt $allowedDifferenceSeconds){
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $testHost
$row.Seconds = $s
Write-Host "Time on" $testHost "outside allowed range"
$row
}
else{
Write-Host "Time on" $testHost "within allowed range"
}
}
Catch { Debug-Output 0 "`$testHost: $testHost"
Write-Debug "`$t: $t "
$dts}
}
Function Debug-Output
{
Param(
[Parameter(Mandatory=$True,Position=1)]
[ValidateRange(0,10)][int]$Level,
[Parameter(Mandatory=$True,Position=2)]
[string]$Message
)
#
# Print Debug output
#
if ( $DebugFlag ) { if ($Level -le $DebugLevel ) {
if ( ! $DebugBG ) {
$a = (Get-Host).PrivateData
$DebugFG = $a.DebugForegroundColor
$DebugBG = $a.DebugBackgroundColor
}
Write-Host -ForegroundColor $DebugFG -BackgroundColor $DebugBG "Debug L${Level}($DebugLevel) $Message"
# -WarningAction Continue -ErrorAction Continue
$WhereFrom = $MyInvocation.ScriptName
$WhereAt = $Myinvocation.ScriptLineNumber
Write-Host -ForegroundColor $DebugFG -BackgroundColor $DebugBG "From: $WhereFrom Line: $WhereAt "
}
}
}