批处理之家's Archiver

yu2n 发表于 2017-2-10 23:48

JS 同步本机时间与网络时间

[code]//文件名称: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);[/code]

yu2n 发表于 2017-2-12 17:03

[i=s] 本帖最后由 yu2n 于 2017-2-13 16:30 编辑 [/i]

[color=Red]更新内容:修复对网络内容使用 eval 的安全问题。[/color][code]//文件名称: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);[/code]

happy886rr 发表于 2017-2-12 18:46

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=195947&ptid=43147]2#[/url] [i]yu2n[/i] [/b]
感谢分享代码,让我学到很多js技巧。外国人的月一直都是加1     getMonth()+1

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.