Windows设置计划任务1分钟校时1次

2024-08-29 15:21:39 浏览数 (1)

设置开机计划任务1分钟校时的原因是开机后第1次校时有时候要等若干分钟,校时成功后才会走公共镜像默认的ntp 5分钟校时,设置开机计划任务就是为了让开机后快速校时,不用等若干分钟(等若干分钟的原因见微软官网文档https://docs.microsoft.com/en-us/troubleshoot/windows-client/identity/w32time-not-start-on-workgroup )

管理员身份powershell运行代码

代码语言:txt复制
schtasks.exe /delete /tn "time sync" /F 2>&1 > $null
schtasks.exe /delete /tn "timesync" /F 2>&1 > $null
$configfile =@"
@echo off`n`r
net stop w32time 2>&1 > nul`n`r
#w32tm /unregister`n`r
#w32tm /register`n`r
sc.exe triggerinfo w32time delete`n`r
sc.exe config w32time start= auto`n`r
sc.exe triggerinfo w32time start/networkon stop/networkoff`n`r
net start w32time`n`r
w32tm /config /manualpeerlist:"time1.tencentyun.com time2.tencentyun.com  time3.tencentyun.com time4.tencentyun.com time5.tencentyun.com"  /syncfromflags:manual /reliable:yes /update`n`r
w32tm /resync`n`r
w32tm /resync`n`r
w32tm /resync`n`r
"@
$configfile|Out-File -FilePath C:Windowstimesync.bat -Encoding ascii -Force
notepad C:Windowstimesync.bat
代码语言:txt复制
#Invoke-WebRequest -uri http://windowsgg-1251783334.cos.na-siliconvalley.myzijiebao.com/timesync.xml -OutFile c:timesync.xml
#Register-ScheduledTask -xml (Get-Content 'c:timesync.xml' | Out-String) -TaskName timesync -TaskPath  -force
#上面2行代码适用≥2012系统,不适用2008R2系统,为了提升代码兼容性,上面2行的功能用下面的黑体代码替代了

$str1=""

$str2=""

$str3=""

if(((Get-WmiObject Win32_OperatingSystem).version) -eq "6.1.7601"){

$str1="certutil -urlcache -split -f "

$str2=" "

$str3="Win7"

}

if(((Get-WmiObject Win32_OperatingSystem).version) -ne "6.1.7601"){

$str1="Invoke-WebRequest -uri "

$str2=" -OutFile "

if(((Get-WmiObject Win32_OperatingSystem).version) -eq "6.3.9600"){$str3="Win8"}

if(((Get-WmiObject Win32_OperatingSystem).version).split(".")[0] -eq "10"){$str3="Win10"}

}

"Set-ExecutionPolicy Unrestricted -force" > c:importtask.ps1

$str1 ("'http://windowsgg-1251783334.cos.na-siliconvalley.myzijiebao.com/timesync.xml'") $str2 ("'c:timesync.xml'") >> c:importtask.ps1

powershell -file c:importtask.ps1

start-sleep 5

del "c:importtask.ps1" 2>&1 > $null

schtasks /create /tn "timesync" /XML c:timesync.xml 2>$null

start-sleep 5

del "c:timesync.xml" 2>&1 > $null

0 人点赞