Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - how to calculate the system uptime

Reply
 
Old 06-08-2009   #1 (permalink)
Heinz


 
 

how to calculate the system uptime

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






My System SpecsSystem Spec
Old 06-08-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: how to calculate the system uptime


"Heinz" <no@xxxxxx> wrote in message
news:%23EAAK5E6JHA.3640@xxxxxx
Quote:

> 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)


My System SpecsSystem Spec
Old 06-08-2009   #3 (permalink)
Heinz


 
 

Re: how to calculate the system uptime


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

>
> "Heinz" <no@xxxxxx> wrote in message
> news:%23EAAK5E6JHA.3640@xxxxxx
Quote:

>> 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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Solved System Uptime Software
Longest System Uptime Overclocking & Cooling
Re: system health and uptime VB Script
Uptime Vista performance & maintenance
Uptime for the System Tutorials


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46