tags:

views:

87

answers:

2

I know how to get Windows system uptime since last reboot in VBScript or C#, but how do you get system uptime in the last 24 hours? The machine could go down multiple times during this period.

A: 

If you don't need the answer down to the second, you could periodically write the current up-time to a file or database record once every 1 or 5 minutes. When the current up-time is less than the persisted up-time, you know you need to move to the next record because the system went down since the last report. Then use the sum of all of the up-time values within a period to determine total up-time for that period. If your up-time spans period boundaries (spans days, for example), you might want to also track the time of the beginning of each up-time record so you know how much of it was before and how much was after the boundary (how much of this up-time record was from yesterday, for example). Or you could simply force starting a new record every time you cross a boundary if your boundary is constant (for example, if your boundaries always represent days within a particular time zone). However, if you have the potential of needing to calculate up-time for the past 24 hours as well as needing to calculate up-time for all of yesterday (which overlaps the past 24 hours) you don't have any fixed boundary along which to do this, so you'll have to just record the start time and up-time and apply boundaries later (as I described first).

Honestly, I haven't tested this, but that's what I would try.

BlueMonkMN
A: 

Use the Event Logs. In the System logs, you should see event #6006 (source: EventLog) when the system goes down, and event #6009 (same source) when it comes back up. See KB article 196452 for more info (note that unexpected shutdowns--that is, crashes--will give event #6008 instead of #6006).

So if you run through all the events of the last 24 hours, your uptime is 24 hours minus all the time between the 6006 and the 6009 events.

ewall