0

Hey guys im im trying to get a cumulative uptime data of my network machines easily.

Essentially we need the total of the uptime saved to a file so at say the end of the quarter we can report on this.

I can easily get uptime at the time i run a script, but unsure how to go about one that saves the uptime of the machine each day so i can then report back on it.

Any help is appreciated.

1 Answers1

0

If you want to do this with the uptime that the machine itself monitors you need to (more or less) continuously update your log, because the counter is reset after a system crash or reboot you'll lose the uptime information between the last log write and the crash/reboot. A better approach would be to implement a system monitoring solution (Nagios, Zabbix, WhatsUp, SCOM, ...). These tools continuously track the system uptime and already provide reports that you can use.

If you still want to go the "local script" route, running something like the following every 5 minutes as a scheduled task might work:

Set fso = CreateObject("Scripting.FileSystemObject")
Set wmi = GetObject("winmgmts://./root/cimv2")
Set re  = New RegExp

logfile = "C:\path\to\uptime.log"

uptimes = fso.OpenTextFile(logfile, 1, True).ReadAll

'extract last uptime from log
re.Pattern = "(\d+)\s*$"
For m In re.Execute(uptimes)
  lastUptime = CLng(m.SubMatches(0))
Next

'get current uptime
qry = "SELECT * FROM Win32_PerfFormattedData_PerfOS_System"
For Each v In wmi.ExecQuery(qry)
  currentUptime = CLng(v.SystemUpTime)
Next

Set f = fso.OpenTextFile(logfile, 2)
If IsEmpty(lastUptime) Or lastUptime >= currentUptime Then
  'append current uptime if the last uptime was greater or equal to than the
  'current uptime (=> a crash or reboot occurred) or no uptime was logged before
  f.WriteLine uptimes & currentUptime
Else
  'update last uptime otherwise
  f.WriteLine re.Replace(uptimes, currentUptime)
End If
f.Close
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328