Functions/PoShMon.OSMonitoring/Test-ComputerTime.ps1

Function Test-ComputerTime
{
    [CmdletBinding()]
    param (
        [hashtable]$PoShMonConfiguration
    )

    if ($PoShMonConfiguration.OperatingSystem -eq $null) { throw "'OperatingSystem' configuration not set properly on PoShMonConfiguration parameter." }

    $mainOutput = Get-InitialOutputWithTimer -SectionHeader "Server Clock Review" -OutputHeaders ([ordered]@{ 'ServerName' = 'Server Name'; 'CurrentTime' = 'Current Time' })

    $results = Get-WmiObject Win32_OperatingSystem -Computername $PoShMonConfiguration.General.ServerNames | `
                    ForEach {
                        return [pscustomobject]@{
                            "PSComputerName" = $_.PSComputerName
                            "DateTime" = $_.ConvertToDateTime($_.LocalDateTime)
                        }
                    } | Sort "DateTime" -Descending
    
    # this is a poor measure - it ignores timezones - but it's good enough for what I need now...
    $difference = [timespan]::new(0)
    
    if ($results.Count -gt 1)
    {
        for ($i=0;$i -lt $results.count - 1;$i++)
            { $difference += $results[$i].DateTime.Subtract($results[$i + 1].DateTime) }
    } else { # if it's just a single server being tested, compare it against the local machine that PoShMon is running on
        $difference += (Get-Date).Subtract($results[0].DateTime)
    } 

    foreach ($serverResult in $results)
    {
        Write-Verbose ($serverResult.PSComputerName + ": " + $serverResult.DateTime.ToShortTimeString())

        $highlight = @()
        
        if ($difference.Minutes -ge $PoShMonConfiguration.OperatingSystem.AllowedMinutesVarianceBetweenServerTimes)
        {
            $mainOutput.NoIssuesFound = $false
            $highlight += "CurrentTime"
        }

        $mainOutput.OutputValues += @{
            'ServerName' = $serverResult.PSComputerName
            'CurrentTime' = $serverResult.DateTime.ToString()
            'Highlight' = $highlight
        }
    }

    return (Complete-TimedOutput $mainOutput)
}