Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for lastboot.vbs

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

  1. Option Explicit
  2.  
  3. ' Declare variables
  4. Dim blnQuiet
  5. Dim intValidArgs
  6. Dim colItems, objItem, objWMIService
  7. Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
  8. Dim strBootMonth, strBootTime, strBootYear, strComputer, strMsg, strQuery
  9.  
  10. intValidArgs = 0
  11.  
  12. ' Check command line parameters
  13. With WScript.Arguments
  14. 	Select Case .Unnamed.Count
  15. 		Case 0
  16. 			' Default if none specified is local computer (".")
  17. 			Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  18. 			strQuery = "Select * from Win32_ComputerSystem"
  19. 			Set colItems = objWMIService.ExecQuery( strQuery, , 48 )
  20. 			For Each objItem in colItems
  21. 				strComputer = objItem.Name
  22. 			Next
  23. 		Case 1
  24. 			' Command line parameter can either be a computer
  25. 			' name or "/?" to request online help
  26. 			strComputer = UCase( Wscript.Arguments(0) )
  27. 		Case Else
  28. 			' Maximum is 1 command line parameter, except for the switch
  29. 			Syntax
  30. 	End Select
  31. 	If .Named.Exists( "Q" ) Then
  32. 		blnQuiet = True
  33. 		intValidArgs = intValidArgs + 1
  34. 	End If
  35. 	If intValidArgs <> .Named.Count Then Syntax
  36. End With
  37.  
  38. ' Enable error handling
  39. 'On Error Resume Next
  40.  
  41. ' Connect to specified computer
  42. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  43. ' Display error number and description if applicable
  44. If Err Then ShowError
  45.  
  46. Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
  47. For Each objItem in colItems
  48. 	If blnquiet Then
  49. 		strMsg = Left( objItem.LastBootUpTime, 12 )
  50. 	Else
  51. 		strBootYear  = Left( objItem.LastBootUpTime, 4 )
  52. 		strBootMonth = Mid( objItem.LastBootUpTime,  5, 2 )
  53. 		strBootDay   = Mid( objItem.LastBootUpTime,  7, 2 )
  54. 		strBootDate  = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear )
  55. 		strBootHour  = Mid( objItem.LastBootUpTime,  9, 2 )
  56. 		strBootMins  = Mid( objItem.LastBootUpTime, 11, 2 )
  57. 		strBootTime  = strBootHour & ":" & strBootMins
  58. 		strBoot = strBootDate & ", " & strBootTime
  59. 		strMsg  = "Last boot time of " & strComputer & ": " & strBoot
  60. 	End If
  61. Next
  62.  
  63. ' Display results
  64. WScript.Echo strMsg
  65.  
  66. 'Done
  67. WScript.Quit(0)
  68.  
  69.  
  70. Sub ShowError()
  71. 	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _
  72. 	       & Err.Description & vbCrLf & vbCrLf & vbCrLf
  73. 	Syntax
  74. End Sub
  75.  
  76.  
  77. Sub Syntax( )
  78. 	strMsg = strMsg _
  79. 	       & "LastBoot.vbs,  Version 2.00" _
  80. 	       & vbCrLf _
  81. 	       & "Display last boot time for any WMI enabled computer" _
  82. 	       & vbCrLf & vbCrLf _
  83. 	       & "Usage:  CSCRIPT  //NoLogo  LASTBOOT.VBS  [ computer_name ]  [ /Q ]" _
  84. 	       & vbCrLf & vbCrLf _
  85. 	       & "Where:  ""computer_name""  is an optional remote computer name" _
  86. 	       & vbCrLf _
  87. 	       & "                         (default is the local computer name)" _
  88. 	       & vbCrLf _
  89. 	       & "        /Q               outputs date/time only, in YYYYMMDDhhmm format" _
  90. 	       & vbCrLf _
  91. 	       & "                         (default is message and DD-MM-YYYY, hh:mm format)" _
  92. 	       & vbCrLf & vbCrLf _
  93. 	       & "Written by Rob van der Woude" & vbCrLf _
  94. 	       & "http://www.robvanderwoude.com"
  95. 	WScript.Echo strMsg
  96. 	WScript.Quit 1
  97. End Sub
  98.  

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