Can anyone explain the format for wmi's LastBootUpTime? GMT time is 5 hours
ahead of my local time yet LastBootUpTime is 5 hours in the opposite
direction! In other words if I boot up at noon, GMT time is 5PM and
BootUpTime is 7AM. What time system is MS using?
In the script below I subtract a _negative_ 300 from the boot up time minutes
to generate 'BootTime' in local time, meaning I ADD 5 hours. In another script
I subtract a _positive_ 300 to convert GMT time to local time, ie SUBTRACT 5
hours. A little confusing to say the least.
An unrelated question: Is there a difference between WScript.CreateObject and
new ActiveXObject? I see both oSh = new ActiveXObject("WScript.Shell") and
oSh = WScript.CreateObject("WScript.Shell") in the docs, and in the script
below I can change both oSh and fso to use WScript.CreateObject and it works
fine. Are the two always interchangeable?
--
Thanks,
Jim
/*
This script has two functions. With no command line argument it shows
the OS's uptime. Or if a program name is given as a command line argument
the program will be timed. To do this, create a shortcut for the script
and add "wscript" before the existing text in Properties|Shortcut|Target
and the name of the program after it.
(for example, wscript "c:\temp\jtime.js" Freecell)
*/
var oSh = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var oArgs = WScript.Arguments;
var t, s, runtime, BootTime, start = new Date();
if (oArgs.length > 0){
oSh.run(oArgs(0), 1, true);
runtime = new Date() - start;
s = fso.GetFileName(oArgs(0)) + " run time: " + GetText(runtime);
Quote:
}
else{
t = GetBootUpTime();
BootTime = new Date(t.substr(0,4), t.substr(4,2)-1, t.substr(6,2),
t.substr(8,2), t.substr(10,2) - t.substr(21), t.substr(12,2));
runtime = start - BootTime;
s = "Booted:\t" + BootTime.toLocaleString() + "\nNow:\t";
s += start.toLocaleString() + "\n\nUp time:\t" + GetText(runtime);
s += "\n\nWMI:\t" + t; //delete this line after testing
Quote:
}
oSh.Popup(s, 0, "jtime", 64);
function GetBootUpTime(){
var wmi = GetObject("winmgmts:");
var objOS = wmi.ExecQuery("select * from Win32_OperatingSystem");
var e = new Enumerator(objOS);
return(e.item().LastBootUpTime);
Quote:
}
function GetText(n){
var s="", nSeconds, nMinutes, nHours, nDays;
var second=1000, minute=60*second, hour=60*minute, day=24*hour;
switch (true){
case n >= day : nDays = Math.floor(n/day);
case n >= hour : nHours = Math.floor(n%day/hour);
case n >= minute : nMinutes = Math.floor(n%hour/minute);
default : nSeconds = Math.floor(n%minute/second);
}
var strDays = (nDays==1)? " day, " : " days, ";
var strHours = (nHours==1)? " hour, " : " hours, ";
var strMinutes = (nMinutes==1)? " minute, " : " minutes, ";
var strSeconds = (nSeconds==1)? " second" : " seconds";
switch (true){
case nDays > 0 : s += nDays + strDays;
case nHours > 0 : s += nHours + strHours;
case nMinutes > 0 : s += nMinutes + strMinutes;
default : s += nSeconds + strSeconds;
}
return s;
Quote:
}