Problem overview
IT professionals often struggle with inconsistent time zones, missing or incorrect NTP configurations, and system clocks that drift out of sync. These issues can cause authentication failures, scheduling confusion, and compliance risks, making reliable system time crucial in any managed environment.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-monitor-system-time-check
#>
# Set the target time zone
$TARGET_TIMEZONE = "{{cf_timezone}}"
# Variables to track failed checks
$check_count = 0
$checks_failed = @()
# Get the current time zone
$current_timezone = (Get-TimeZone).Id
Write-Output "Current Time Zone: $current_timezone"
# Check if the time zone matches the target
if ($current_timezone -ne $TARGET_TIMEZONE) {
Write-Output "ALERT: Time zone is not $TARGET_TIMEZONE. Attempting to set it..."
try {
Set-TimeZone -Id $TARGET_TIMEZONE
Write-Output "Time zone set to $TARGET_TIMEZONE successfully."
} catch {
Write-Output "Failed to set time zone to $TARGET_TIMEZONE."
$check_count++
$checks_failed += "Failed to set time zone to $TARGET_TIMEZONE."
}
}
# Check if the system clock is synchronized
$clock_sync = w32tm /query /status | Select-String "Stratum"
if ($clock_sync -match "Stratum:\s+(\d+)" -and [int]$matches[1] -eq 0) {
Write-Output "ALERT: System clock is not synchronized."
$check_count++
$checks_failed += "System clock is not synchronized."
}
# Check if an NTP server is properly configured
$ntp_server = w32tm /query /configuration | Select-String "NtpServer" | Select-String -NotMatch "NtpServer: (Local|.*,0x[0-9A-F]+$)"
if (-not $ntp_server) {
Write-Output "ALERT: No NTP server is properly configured."
$check_count++
$checks_failed += "No NTP server is configured."
}
# Final check summary
if ($check_count -gt 0) {
Write-Output "ALERT: $check_count checks failed."
Write-Output "Checks Failed: $($checks_failed -join ', ')"
exit 1
} else {
Write-Output "SUCCESS: All checks passed."
exit 0
}
This script checks the current time zone against the intended target and attempts to apply the correct setting if there is a mismatch. It also verifies that the system clock is synchronized, detects whether an NTP server is properly configured, and logs any failures or successes so you can take timely action. By ensuring accurate system time, this resource helps streamline operations and reduce performance or security bottlenecks tied to incorrect clocks.
Use cases
- Monitoring Windows endpoints that frequently change time zones or have known clock drift issues
- Maintaining compliance in environments where precise time synchronization is essential
- Ensuring critical applications reliant on accurate timestamps run without disruption
- Quickly identifying and correcting missing or misconfigured NTP settings
Recommendations
- Configure a script-based monitor in Level to run this check whenever time consistency issues are suspected
- For scheduled checks, create an automation in Level with a schedule trigger to run this script regularly
- Test in a non-production environment first to confirm no unexpected impacts occur
- Verify that the specified time zone matches your local or organizational requirements
- Use Level’s system-level permissions to minimize the risk of permission-related errors