前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Windows设置计划任务1分钟校时1次

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

原创
作者头像
Windows技术交流
修改2024-04-07 11:25:25
1.5K0
修改2024-04-07 11:25:25
举报
文章被收录于专栏:Windows技术交流Windows技术交流

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

管理员身份powershell运行代码

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:\Windows\timesync.bat -Encoding ascii -Force notepad C:\Windows\timesync.bat

#Invoke-WebRequest -uri http://windowsgg-1251783334.cos.na-siliconvalley.myqcloud.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.myqcloud.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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档