![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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) > > thx Heinz |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
System Uptime | Software | |||
| Longest System Uptime | Overclocking & Cooling | |||
| Re: system health and uptime | VB Script | |||
| Uptime | Vista performance & maintenance | |||
| Uptime for the System | Tutorials | |||