Where Customization Begins 

  • System Uptime with VBS script

 #27933  by TheAslan
 05 Sep 2013, 22:12
Here's one very neat trick how to show your current System Uptime and also the last time your computer was booted.

Image

First open Notepad, then copy/paste following text thereto and save it as Uptime.vbs or whatever you like, then just double click it.

Code: Select allOption Explicit
     Dim objOS, dateLastBoot, nSystemUptime
     Dim nDays, nHours, nMinutes
     '
     ' Get the Windows Home Server OS object
     '
     For Each objOS in GetObject( _
         "winmgmts:").InstancesOf ("Win32_OperatingSystem")
         '
         ' Return the last boot up time and
         ' convert it to a Date object
         '
         dateLastBoot = ConvertToDate(objOS.LastBootUpTime)
         '
         ' Calculate the number of minutes between then and now
         '
         nSystemUptime = DateDiff("n", dateLastBoot, Now)
         '
         ' Convert the total minutes into hours, days, and minutes
         '
         nDays = Int(nSystemUptime / 1440)
         nHours = Int (((nSystemUptime / 1440) - nDays) * 24)
         nMinutes = nSystemUptime Mod 60
         '
         ' Display the result
         '
         Wscript.Echo "Last Boot: " & dateLastBoot & vbCrLf & _
                      "System Uptime: " & _
                      nDays & " days and " & _
                      nHours & " hours and " & _
                      nMinutes & " minutes"
     Next
     '
     ' This function takes a datetime string and converts
     ' it to a real date and time object
     '
     Function ConvertToDate(strDate)
         Dim strYear, strMonth, strDay
         Dim strHour, strMinute, strSecond
         strYear = Left(strDate, 4)
         strMonth = Mid(strDate, 5, 2)
         strDay = Mid(strDate, 7, 2)
         strHour = Mid(strDate, 9, 2)
         strMinute = Mid(strDate, 11, 2)
         strSecond = Mid(strDate, 13, 2)
         ConvertToDate = DateSerial(strYear, strMonth, strDay) & " " & _
                    TimeSerial(strHour, strMinute, strSecond)
End Function


Credits goes to allcomputers.us