标题: [原创代码] JS 同步本机时间与网络时间 [打印本页]
作者: yu2n 时间: 2017-2-10 23:48 标题: JS 同步本机时间与网络时间
- //文件名称:SyncNetTime.js
- //功能说明:同步本机时间与网络时间
- //使用方法:Cscript.exe //nologo SyncNetTime.js
- //测试环境:系统 Win10 x64 时间 17/2/10 23:35 用户 Yu2n
-
- //以管理员运行
- function GetSystemVersion() {
- var os = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem");
- for (var e = new Enumerator(os); ! e.atEnd(); e.moveNext()) {
- var v = e.item().Version;
- var ss = v.split('.');
- return ss[0] + ss[1];
- }
- return - 1;
- }
- if (GetSystemVersion() >= 60) {
- var cmd = WScript.ScriptFullName;
- if (cmd.substring(cmd.length - 3) != ".jS") {
- var Shell = new ActiveXObject("Shell.Application");
- Shell.ShellExecute("wscript.exe", "\"" + cmd.substring(0, cmd.length - 3) + ".jS\"", "", "runas", 1);
- WScript.Quit(0);
- }
- }
-
- //获取网络时间,从 www.beijing-time.org 页面
- var getNetDate = function() {
- var s='';
- try{
- var http = new ActiveXObject("Microsoft.XMLHTTP");
- http.open("GET", "http://www.beijing-time.org/time15.asp?rnd=" + (new Date()), false);
- http.send();
- s = http.responseText;
- }catch(e){};
- if (/^t0\=[\s\S]+nyear\=[\s\S]+nsec\=\d+\;$/igm.test(s) == true) {
- eval(s);
- return new Date(nyear+'/'+nmonth+'/'+nday+' '+nhrs+':'+nmin+':'+nsec);
- } else {
- WScript.Echo("警告:获取网络时间失败!")
- WScript.Quit(0);
- }
- };
-
- //设置时间
- function ChangeDate()
- {
- var WmiService, ComputerName, OSList, OSEnum, OS, DateTime;
- ComputerName = ".";
- WmiService = GetObject ("winmgmts:{impersonationLevel=impersonate, (Systemtime)}!//" + ComputerName + "/root/cimv2");
- OSList = WmiService.InstancesOf ("Win32_OperatingSystem");
- DateTime = new ActiveXObject ("WbemScripting.SWbemDateTime");
- OSEnum = new Enumerator (OSList);
- for ( ; !OSEnum.atEnd(); OSEnum.moveNext())
- {
- OS = OSEnum.item();
- var dtNewDate = getNetDate(); //获取网络时间
- DateTime.Value = OS.LocalDateTime;
- DateTime.Year = dtNewDate.getFullYear();
- DateTime.Month = dtNewDate.getMonth()+1;
- DateTime.Day = dtNewDate.getDate();
- DateTime.Hours = dtNewDate.getHours();
- DateTime.Minutes = dtNewDate.getMinutes();
- DateTime.Seconds = dtNewDate.getSeconds();
- if (OS.SetDateTime(DateTime.Value) != 0)
- WScript.Echo("警告:设置系统时间失败!");
- else
- WScript.Echo("提示:设置成功。当前时间:" + new Date(DateTime.GetVarDate()).toLocaleString());
- }
- }
- ChangeDate();
- WScript.Quit(0);
复制代码
作者: yu2n 时间: 2017-2-12 17:03
本帖最后由 yu2n 于 2017-2-13 16:30 编辑
更新内容:修复对网络内容使用 eval 的安全问题。- //文件名称:SyncNetTime.js
- //功能说明:同步本机时间与网络时间
- //使用方法:Cscript.exe //nologo SyncNetTime.js
- //测试环境:系统 Win10 x64 时间 17/2/12 17:00 用户 Yu2n
- //更新内容:Fix 获取网络时间,防止 eval 安全问题
-
- //以管理员运行
- function GetSystemVersion() {
- var os = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem");
- for (var e = new Enumerator(os); ! e.atEnd(); e.moveNext()) {
- var v = e.item().Version;
- var ss = v.split('.');
- return ss[0] + ss[1];
- }
- return - 1;
- }
- if (GetSystemVersion() >= 60) {
- var cmd = WScript.ScriptFullName;
- if (cmd.substring(cmd.length - 3) != ".jS") {
- var Shell = new ActiveXObject("Shell.Application");
- Shell.ShellExecute("wscript.exe", "\"" + cmd.substring(0, cmd.length - 3) + ".jS\"", "", "runas", 1);
- WScript.Quit(0);
- }
- }
-
- //获取网络时间,从 www.beijing-time.org 页面,使用正则验证结果,防止 eval 安全问题
- var getNetDate = function() {
- var s='';
- try{
- var http = new ActiveXObject("Microsoft.XMLHTTP");
- http.open("GET", "http://www.beijing-time.org/time15.asp?rnd=" + (new Date()), false);
- http.send();
- s = http.responseText;
- }catch(e){};
- var re = /^[\s\S]*(nyear\=\d+;[\r\n]+)(nmonth\=\d+;[\r\n]+)(nday\=\d+;[\r\n]+)(nwday\=\d+;[\r\n]+)(nhrs\=\d+;[\r\n]+)(nmin\=\d+;[\r\n]+)(nsec\=\d+;)[\s\S]*$/igm;
- if (re.test(s) == true) {
- eval(s.replace(re,'$1$2$3$5$6$7')); //使用正则替换结果,防止 eval 安全问题
- return new Date(nyear+'/'+nmonth+'/'+nday+' '+nhrs+':'+nmin+':'+nsec);
- } else {
- WScript.Echo("警告:获取网络时间失败!")
- WScript.Quit(0);
- };
- };
-
- //设置时间
- function ChangeDate()
- {
- var WmiService, ComputerName, OSList, OSEnum, OS, DateTime;
- ComputerName = ".";
- WmiService = GetObject ("winmgmts:{impersonationLevel=impersonate, (Systemtime)}!//" + ComputerName + "/root/cimv2");
- OSList = WmiService.InstancesOf ("Win32_OperatingSystem");
- DateTime = new ActiveXObject ("WbemScripting.SWbemDateTime");
- OSEnum = new Enumerator (OSList);
- for ( ; !OSEnum.atEnd(); OSEnum.moveNext())
- {
- OS = OSEnum.item();
- var dtNewDate = getNetDate(); //获取网络时间
- DateTime.Value = OS.LocalDateTime;
- DateTime.Year = dtNewDate.getFullYear();
- DateTime.Month = dtNewDate.getMonth() + 1;
- DateTime.Day = dtNewDate.getDate();
- DateTime.Hours = dtNewDate.getHours();
- DateTime.Minutes = dtNewDate.getMinutes();
- DateTime.Seconds = dtNewDate.getSeconds();
- if (OS.SetDateTime(DateTime.Value) != 0) {
- WScript.Echo("警告:设置系统时间失败!");
- } else {
- WScript.Echo("提示:设置成功。当前时间:" + new Date(DateTime.GetVarDate()).toLocaleString());
- };
- }
- }
- ChangeDate();
- WScript.Quit(0);
复制代码
作者: happy886rr 时间: 2017-2-12 18:46
回复 2# yu2n
感谢分享代码,让我学到很多js技巧。外国人的月一直都是加1 getMonth()+1
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |