回复 15# ygqiang
妹阿,讓你自己刪,可也得保證JS語句正常阿,你刪得 if else 都不能保持正常姿勢了。
以下是注釋掉提示的代碼:- //文件名称:SyncNetTime.js
- //功能说明:同步本机时间与网络时间
- //使用方法:Cscript.exe //nologo SyncNetTime.js
- //测试环境:系统 Win10 x64 时间 17/2/10 23:35 用户 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);
复制代码
|