Re: Up time stats?

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 07/09/04


Date: Fri, 09 Jul 2004 19:35:35 +0200

neo [mvp outlook] wrote:

> Nope. For remote stuff, I would use WMI to pull the information.
>
> This link has a WSH/VBScript file that can be modified to possible suit your
> needs.
> http://groups.google.com/groups?selm=3E841B1E.66A667C1%40hydro.com
Hi

In the script in the link above I didn't handle the time zone information
correctly, here is an updated version that should work better:

strComputer = "." ' "." for local computer

On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer _
      & "\root\cimv2")

If Err.Number = 0 Then
      On Error Goto 0
      Set colOperatingSystems = objWMIService.ExecQuery _
          ("Select * from Win32_OperatingSystem")

      For Each objOS in colOperatingSystems
          dtmLastBootUpTime = ConvWMIDateTime(objOS.LastBootUpTime, "ISO8601")
          dtmSystemUptimeInHours = DateDiff("h", dtmLastBootUpTime, Now)
          dtmSystemUptimeInDays = DateDiff("d", dtmLastBootUpTime, Now)
          Wscript.Echo "Computer: " & strComputer _
                       & " started at " & dtmLastBootUpTime
          Wscript.Echo "Uptime in hours: " & dtmSystemUptimeInHours
          Wscript.Echo "Uptime in days: " & dtmSystemUptimeInDays & vbCrlf
      Next

Else
      Wscript.Echo "Could not connect to computer with WMI: " _
          & strComputer
End If

Function ConvWMIDateTime(sDMTFformat, iNamedFormat)

' Author: Torgeir Bakken
' Modified: 2004-01-16
'
' Converts WMI Date and Time Format to standard date/time
'
' WMI Date and Time Format is documented here:
' http://msdn.microsoft.com/library/en-us/wmisdk/wmi/date_and_time_format.asp
'
' Arguments ----->

' DMTFformat: Date string in WMI Date and Time Format
'
' iNamedFormat: Optional. Numeric value that indicates the date/time
' format used. If omitted, ISO 8601 is used.

' -1 (and anything <> 0-4) Display in International Date Format ISO8601
' YYYY-MM-DD hh:nn:ss e.g. 2004-01-15 23:50:44
'
' vbGeneralDate 0 Display a date and/or time. If there is a date part,
' display it as a short date. If there is a time part,
' display it as a long time. If present, both parts are
' displayed.
'
' vbLongDate 1 Display a date using the long date format specified in
' your computer's regional settings.
'
' vbShortDate 2 Display a date using the short date format specified in
' your computer's regional settings.
'
' vbLongTime 3 Display a time using the time format specified in your
' computer's regional settings.
'
' vbShortTime 4 Display a time using the 24-hour format (hh:mm).

    Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
    sYear = mid(sDMTFformat, 1, 4)
    sMonth = mid(sDMTFformat, 5, 2)
    sDay = mid(sDMTFformat, 7, 2)
    sHour = mid(sDMTFformat, 9, 2)
    sMinutes = mid(sDMTFformat, 11, 2)
    sSeconds = mid(sDMTFformat, 13, 2)

    ' YYYY-MM-DD hh:nn:ss
    ConvWMIDateTime = sYear & "-" & sMonth & "-" & sDay & " " _
                 & sHour & ":" & sMinutes & ":" & sSeconds

    If IsNumeric(iNamedFormat) Then
      If iNamedFormat >= 0 And iNamedFormat <= 4 Then
        ' FormatDateTime will set date format to specified format
        ConvWMIDateTime = FormatDateTime(ConvWMIDateTime, iNamedFormat)
      End If
    End If
End Function

-- 
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx


Relevant Pages

  • Re: 2 questions: uptime and last logged on users
    ... > For your computer uptime question: ... > ' Converts WMI Date and Time Format to standard date/time ... > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.win32.programmer.wmi)
  • Re: 2 questions: uptime and last logged on users
    ... > Anybody where to look for the computer uptime, and where to look for a list ... ' Converts WMI Date and Time Format to standard date/time ... ' vbGeneralDate 0 Display a date and/or time. ...
    (microsoft.public.win32.programmer.wmi)
  • Re: Up time stats?
    ... I would use WMI to pull the information. ... > On Error Goto 0 ... > ' Converts WMI Date and Time Format to standard date/time ... > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.win2000.general)
  • Re: Rename a Mapped Drive using WMI
    ... I must rename a mapped drive using WMI. ... and display "Access ... Denied" even with an Administrator account. ... torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.scripting.wsh)
  • Re: WMI Date works on XP but not 2000 Server
    ... You will need to build the time format yourself, see example below, ... If TZOffset < 0 Then ... Function TimeZoneOffset ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.windows.server.security)

Quantcast