-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_remoteserver_time.ps1
More file actions
76 lines (66 loc) · 2.28 KB
/
check_remoteserver_time.ps1
File metadata and controls
76 lines (66 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<#
.SYNOPSIS
Compares the system time of remote servers to the local machine.
.DESCRIPTION
This script reads a list of Windows servers from a JSON inventory file,
connects to each one via PowerShell remoting, and compares their system time
against the local system. Results show time difference and a status
indicating whether it's within the acceptable range (±2 minutes).
.PARAMETER InventoryPath
Path to the JSON file containing the server list.
.PARAMETER Credential
User credential for authenticating remote sessions.
.EXAMPLE
PS> $cred = Get-Credential
PS> .\Check-RemoteTimeSync.ps1 -InventoryPath "C:\Path\inventory.json" -Credential $cred
#>
param (
[Parameter(Mandatory = $true)]
[string]$InventoryPath,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]$Credential
)
# Load server inventory
try {
$inventory = Get-Content -Raw -Path $InventoryPath | ConvertFrom-Json
} catch {
Write-Error "Failed to read inventory file: $_"
exit 1
}
# Initialize results
$results = @()
$localTime = Get-Date
# Loop through each server
foreach ($server in $inventory.Abris.windows) {
try {
# Start remote session
$session = New-PSSession -ComputerName $server -Credential $Credential
# Get remote system time
$remoteTime = Invoke-Command -Session $session -ScriptBlock { Get-Date }
# Compare time difference
$timeDiff = [math]::Round((New-TimeSpan -Start $remoteTime -End $localTime).TotalMinutes, 2)
$isSynced = [math]::Abs($timeDiff) -le 2
$status = if ($isSynced) { "Synced" } else { "Out of Sync" }
# Add result
$results += [PSCustomObject]@{
Server = $server
RemoteTime = $remoteTime
LocalTime = $localTime
DiffMin = $timeDiff
Status = $status
}
# Clean up
Remove-PSSession -Session $session
} catch {
Write-Warning "Failed to connect to ${server}: $_"
$results += [PSCustomObject]@{
Server = $server
RemoteTime = "N/A"
LocalTime = $localTime
DiffMin = "N/A"
Status = "Unreachable"
}
}
}
# Output result as table
$results | Format-Table -AutoSize