Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for uptime.vbs

(view source code of uptime.vbs as plain text)

  1. ' Enable custom error handling
  2. On Error Resume Next
  3.  
  4. ' Declare variables
  5. Dim numUptime, numUptMins, numUptHour, numUptDays
  6. Dim strYear, strMonth, strDay, strDate, strHour, strMins, strTime
  7. Dim strComputer, strMsg
  8.  
  9. ' Check command line parameters
  10. If WScript.Arguments.Named.Count > 0 Then
  11. 	Syntax
  12. End If
  13. Select Case WScript.Arguments.Unnamed.Count
  14. 	Case 0
  15. 		strComputer = "."
  16. 	Case 1
  17. 		strComputer = Wscript.Arguments(0)
  18. 	Case Else
  19. 		Syntax
  20. End Select
  21.  
  22. ' Connect to the specified computer
  23. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  24. ' Display error number and description if applicable
  25. If Err Then ShowError
  26.  
  27. ' Get the "real" computer name
  28. Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  29. ' Display error number and description if applicable
  30. If Err Then ShowError
  31. For Each objItem in colItems
  32. 	strComputer = objItem.Name
  33. Next
  34.  
  35. ' Get the uptime by querying the boot time and the current time
  36. Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
  37. For Each objItem in colItems
  38. 	numUptime  = DateDiff( "n", _
  39. 	             ParseDat( objItem.LastBootUpTime ), _
  40. 	             ParseDat( objItem.LocalDateTime ) )
  41. 	numUptMins = numUptime Mod 60
  42. 	numUptHour = ( numUptime \ 60 ) Mod 24
  43. 	numUptDays = ( numUptime \ 60 )  \  24
  44. 	' Build message to be displayed
  45. 	' Day(s)
  46. 	strMsg     = vbCrLf & strComputer & " has been up for " & numUptDays & " day"
  47. 	' Add "s" for multiple days
  48. 	If numUptDays <> 1 Then strMsg = strMsg & "s"
  49. 	' Hour(s)
  50. 	strMsg     = strMsg & ", " & numUptHour & " hour"
  51. 	' Add "s" for multiple hours
  52. 	If numUptHour <> 1 Then strMsg = strMsg & "s"
  53. 	' And minute(s)
  54. 	strMsg     = strMsg & " and " & numUptMins & " minute"
  55. 	' Add "s" for multiple minutes
  56. 	If numUptMins <> 1 Then strMsg = strMsg & "s"
  57. Next
  58.  
  59. ' Display the result
  60. Wscript.Echo strMsg
  61.  
  62.  
  63. Function ParseDat( ByVal strDate )
  64. 	strYear  = Left( strDate, 4 )
  65. 	strMonth = Mid( strDate,  5, 2 )
  66. 	strDay   = Mid( strDate,  7, 2 )
  67. 	strDat   = strDay & "-" & strMonth & "-" & strYear
  68. 	strHour  = Mid( strDate,  9, 2 ) - strTimeShift
  69. 	strMins  = Mid( strDate, 11, 2 )
  70. 	strTime  = strHour & ":" & strMins
  71. 	ParseDat = strDat & " " & strTime
  72. End Function
  73.  
  74.  
  75. Sub ShowError
  76. 	strMsg = strMsg & vbCrLf _
  77. 	       & "Error " & CStr( Err.Number ) _
  78. 	       & ": " & Err.Description & vbCrLf
  79. 	Err.Clear
  80. 	Syntax
  81. End Sub
  82.  
  83.  
  84. Sub Syntax
  85. 	strMsg = strMsg & vbCrLf _
  86. 	       & "UpTime.vbs,  Version 1.00" & vbCrLf _
  87. 	       & "Display uptime for any WMI enabled " _
  88. 	       & "computer on the network." & vbCrLf & vbCrLf _
  89. 	       & "Usage:  CSCRIPT  UPTIME.VBS  [ computer_name ]" _
  90. 	       & vbCrLf & vbCrLf _
  91. 	       & "Where:  " & Chr(34) & "computer_name" & Chr(34) _
  92. 	       & " is the name of a WMI enabled computer on the network" _
  93. 	       & vbCrLf & vbCrLf _
  94. 	       & "Written by Rob van der Woude" & vbCrLf _
  95. 	       & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _
  96. 	       & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _
  97. 	       & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _
  98. 	       & "    FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en" & vbCrLf
  99. 	WScript.Echo strMsg
  100. 	WScript.Quit(1)
  101. End Sub
  102.  

page last modified: 2024-04-16; loaded in 0.0202 seconds