-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeConfig.ps1
147 lines (142 loc) · 5.23 KB
/
timeConfig.ps1
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<#
.SYNOPSIS
Sets Cloudflare as the NTP server
.DESCRIPTION
(c) Danny Murphy. All rights reserved.
Script provided as-is without any warranty of any kind. Use it freely at your own risks.
Must be run with elevated permissions.
Designed to be run as user assigned PowerShell Script from Intune
The script will set Cloudflare as the time source
Requires PowerShell 3.0.
.INPUTS
None
.OUTPUTS
Log file stored in %SystemDrive%\Windows\TEMP\log_Update-timeConfig.txt
.NOTES
Version: 2.0
Author: Danny Murphy
Twitter: @dltmurphy
Creation Date: 19 September 2021
Purpose/Change: Intune managed devices with Cloudflare as the time source
.EXAMPLE
.\timeConfig.ps1
Run as an administator or SYSTEM
#>
#Requires -Version 3
#Requires -Runasadministrator
# Stop if error
$ErrorActionPreference = "stop"
# Variables
$NtpServer = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\).NtpServer
$type = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\).Type
$service = get-service -name "Windows Time"
$logPath = Join-path -path $($env:SystemRoot) -ChildPath "\TEMP\log_timeConfig.txt"
$FrequencyCorrectRate = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config\).FrequencyCorrectRate
$SpecialPollInterval = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient).SpecialPollInterval
$UpdateInterval = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config\).UpdateInterval
$MaxPollInterval = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config\).MaxPollInterval
$MinPollInterval = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config\).MinPollInterfal
Start-Transcript $logPath -Force
try {
w32tm /config /manualpeerlist:"time.cloudflare.com,0x1" /syncfromflags:MANUAL /update
try {
if ($service.StartType -ne "Automatic" ) {
Set-Service w32time -StartupType "Automatic"
return
}
}
catch [SystemException] {
Write-Host "Error processing Windows Time service."
}
try {
if ($service.Status -ne "Running") {
Start-Service "Windows Time"
return
} else {
Restart-Service "Windows Time"
return
}
}
catch [SystemException] {
write-host "Error processing Windows Time service."
}
w32tm /resync
try {
if ($NtpServer -ne "time.cloudflare.com,0x1") {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" -Name "NtpServer" -Type "String" -Value "time.cloudflare.com,0x1" -Force
return
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($type -ne "NTP") {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" -Name "Type" -Type "String" -Value "NTP" -Force
return
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($FrequencyCorrectRate -ne 2) {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "FrequencyCorrectRate" -Value "2"
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($MinPollInterval -ne 64) {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "MinPollInterval" -Value "64"
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($MaxPollInterval -ne 64) {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "MaxPollInterval" -Value "64"
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($UpdateInterval -ne 100) {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "UpdateInterval" -Value "100"
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($SpecialPollInterval -ne 64) {
Set-ItemProperty -Path Registry::"HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient" -Name "SpecialPollInterval" -Value "64"
}
}
catch [System.Management.Automation.PSArgumentException] {
Write-Host "Unable to write to registry"
}
try {
if ($service.Status -ne "Running") {
Start-Service "Windows Time"
return
} else {
Restart-Service "Windows Time"
return
}
}
catch [SystemException] {
write-host "Error processing Windows Time service."
}
}
catch {
Write-Host "An error has occurred"
}
finally {
$ErrorActionPreference = "continue"
Stop-Transcript
w32tm /resync
}