-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindows.psm1
More file actions
73 lines (55 loc) · 1.74 KB
/
Windows.psm1
File metadata and controls
73 lines (55 loc) · 1.74 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
<#
.SYNOPSIS
Gets the last successful time synchronization time.
.OUTPUTS
System.DateTime
Returns the last successful time synchronization time, or the Unix epoch if the time could not be parsed.
.EXAMPLE
Get-LastSyncTime
Gets the last successful time synchronization time.
#>
function Get-LastSyncTime {
[OutputType([DateTime])]
param()
process {
$Regex = '^Last Successful Sync Time: (?<DateTime>[\d/:APM\s]+)$';
$Result = w32tm /query /status | Select-String -Pattern $Regex;
Try {
$LastSyncTime = [DateTime]::Parse($Result.Matches[0].Groups['DateTime'].Value);
} Catch {
$LastSyncTime = Get-Date -Year 1970 -Month 1 -Day 1;
}
return $LastSyncTime;
}
}
<#
.SYNOPSIS
Syncs the system time with the default time server if the time is out of the supplied threshold.
.PARAMETER Threshold
The time threshold to check against. Default is 7 days.
.OUTPUTS
System.Boolean
Returns $True if the system time was out of sync and was successfully synced, otherwise $False.
.EXAMPLE
Sync-Time -Threshold (New-TimeSpan -Days 1)
Syncs the system time if the time is out of sync by more than 1 day.
.EXAMPLE
Sync-Time
Syncs the system time if the time is out of sync by more than the default 7 days.
#>
function Sync-Time {
[OutputType([System.Boolean])]
param(
[ValidateNotNullOrEmpty()]
[timespan]$Threshold = (New-TimeSpan -Days 7)
)
process {
[DateTime]$LastSyncTime = Get-LastSyncTime;
[DateTime]$CurrentTime = Get-Date;
if (($CurrentTime - $LastSyncTime) -gt $Threshold) {
w32tm /resync /force;
return $True;
}
return $False;
}
}