Windows 更新系统时间脚本
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
import subprocess
import sys
import ctypes
NTP_SERVERS = "time.kriss.re.kr,0x8 time.windows.com,0x8 pool.ntp.org,0x8"
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run(cmd):
print(f">>> {cmd}")
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, encoding='gbk', errors='ignore')
print(r.stdout)
if r.returncode != 0:
print(f"[err] {r.stderr}")
return r.returncode == 0
def main():
if not is_admin():
print("❌ 需要管理员权限,正在重新请求...")
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
return
print("=" * 50)
print("Windows 时间同步(韩国 NTP 优先)")
print("=" * 50)
run("net start w32time")
run(f'w32tm /config /manualpeerlist:"{NTP_SERVERS}" /syncfromflags:manual /update')
ok = run("w32tm /resync /force")
print("\n--- 当前状态 ---")
run("w32tm /query /status")
print("\n✅ 完成" if ok else "\n⚠️ 同步失败,检查网络或防火墙")
input("\n按回车关闭...")
if __name__ == "__main__":
main()
This post is licensed under CC BY 4.0 by the author.