Re: how to calculate the system uptime




"Pegasus [MVP]" <news@xxxxxxxxxxxxx> schrieb im Newsbeitrag
news:ePzGnOF6JHA.3592@xxxxxxxxxxxxxxxxxxxxxxx

"Heinz" <no@xxxxxxxx> wrote in message
news:%23EAAK5E6JHA.3640@xxxxxxxxxxxxxxxxxxxxxxx
Hello,

I need a script that will calculate the system uptime.

I can get the LastRebootTime of a machine in "YYYYMMDDDHHMMSS" format
using the last "lastboottime" property from the Win32_OperatingSystem
class.

My idea was to basically do something like "now() - lastbootime" using
DateDiff....

But it seems that it is not so easy to write some code to calculate the
uptime, because of different date\time formats used in different
languages

For example
USA : 4/26/2009 9:36:04 PM
UK : 4/26/2009 21:36:04
France : 26/4/2009 21:36:04
Germany : 26.4.2009 21:36:04

etc.


I would need the calculate the uptime for all machines in minutes,
independent from local time setups

But with all the combinations of "." and "/" "AM/PM" and 24h formats
I keeping getting wrong results....

Maybe it would help if I could find out what date\time setup is used, for
example reading the settings from Controlpanel\RegionalSetting... so that
I could adapt the StringToDate() function in my code...?

Perfect would be solution that works with Win2000 and XP



This is the code that I am currently using (works for US... but not
neccessarily for other regional date\time settings...)


strComputer = "."
Set objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
set colSettings = objWMIService.ExecQuery ("SELECT * FROM
Win32_OperatingSystem")

For Each objOperatingSystem in colSettings

str_LastBootupTime = objOperatingSystem.LastBootUpTime '
YYYYMMDDHHMMSS.milliseconds"
str_LastBootupTime = left (str_LastBootupTime,
instr(str_LastBootupTime,".")-1) ' remove milliseconds

date_LastBootupTime = StringToDate(str_LastBootupTime)
SystemUptime = DateDiff("n", date_lastBootUpTime, Now)
'difference in Minutes

wscript.echo "Uptime in minutes=" & SystemUptime

next

'*********************************************************************************
Function StringToDate(LastBootUpTime)
'
'** Converts LastBootUpTime "YYYYMMDDHHMMSS" to "MM/DD/YYYY
HH:MM:SS"
'
'** returns : DateTime format of "MM/DD/YYYY HH:MM:SS AM/PM"

StringToDate = _
CDate(Mid(LastBootUpTime, 5, 2) & "/" & _
Mid(LastBootUpTime, 7, 2) & "/" & _
Left(LastBootUpTime, 4) & " " & _
Mid (LastBootUpTime, 9, 2) & ":" & _
Mid(LastBootUpTime, 11, 2) & ":" & _
Mid(LastBootUpTime, 13, 2))
End Function



thank you
Heinz

The problem occurs because you're using WMI to obtain the most recent boot
time and the inbuilt now() function to get the System Time. You can avoid
the issue by using WMI for both, e.g. like so:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")
For Each objItem in colItems
Wscript.Echo "Month: " & objItem.Month
Wscript.Echo "Day: " & objItem.Day
Wscript.Echo "Year: " & objItem.Year
Wscript.Echo "Hour: " & objItem.Hour
Wscript.Echo "Minute: " & objItem.Minute
Wscript.Echo "Second: " & objItem.Second
Next

(Code courtesy of the Scripting Guy)



thank you for this interessting hint - I will try to puzzle it together :-)

thx
Heinz


.



Relevant Pages

  • Re: how to calculate the system uptime
    ... I need a script that will calculate the system uptime. ... I can get the LastRebootTime of a machine in "YYYYMMDDDHHMMSS" format ... using the last "lastboottime" property from the Win32_OperatingSystem ...
    (microsoft.public.scripting.vbscript)
  • how to calculate the system uptime
    ... I need a script that will calculate the system uptime. ... I can get the LastRebootTime of a machine in "YYYYMMDDDHHMMSS" format using ... the last "lastboottime" property from the Win32_OperatingSystem class. ... could adapt the StringToDate() function in my code...? ...
    (microsoft.public.scripting.vbscript)
  • Re: how to calculate the system uptime
    ... I can get the LastRebootTime of a machine in "YYYYMMDDDHHMMSS" format using ... the last "lastboottime" property from the Win32_OperatingSystem class. ... If the uptime is guaranteed less than about a month, ...
    (microsoft.public.scripting.vbscript)
  • Re: how to calculate the system uptime
    ... I need a script that will calculate the system uptime. ... I can get the LastRebootTime of a machine in "YYYYMMDDDHHMMSS" format ... the last "lastboottime" property from the Win32_OperatingSystem class. ...
    (microsoft.public.scripting.vbscript)
  • Re: Getting system uptime on Windows
    ... But still I can´t find any way to get the system uptime. ... TCL_DECLARE_MUTEXstatic int initialized = 0; ... return TCL_ERROR;} void Uptime_ExitProc{/* be festideous */ ...
    (comp.lang.tcl)

Quantcast