Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for lastboot.bat

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

  1. @ECHO OFF
  2. :: Keep variables local
  3. SETLOCAL
  4.  
  5. :: Check availability of WMIC.EXE
  6. WMIC.EXE /? >NUL 2>&1 || GOTO Syntax
  7.  
  8. :: Check if help was requested
  9. ECHO.%* | FIND "?" >NUL 2>&1 && GOTO Syntax
  10.  
  11. :: Default: local computer
  12. SET Node=%ComputerName%
  13.  
  14. :: If specified, use remote computer name.
  15. :: If remote computer cannot be reached, return a string of 0s.
  16. IF NOT "%~1"=="" IF /I NOT "%~1"=="/Q" IF NOT "%~1"=="/?" (
  17. 	SET Node=%~1
  18. 	PING %1 -n 2 2>NUL | FIND "TTL=" >NUL
  19. 	IF ERRORLEVEL 1 (
  20. 		SET LastBoot=000000000000
  21. 		GOTO :Display
  22. 	)
  23. )
  24.  
  25. :: Query the last boot time of the specified computer
  26. FOR /F %%A IN ('ECHO exit^| WMIC.EXE /Node:%Node% /Output:STDOUT Path Win32_OperatingSystem Get LastBootUpTime /Format:TABLE') DO SET LastBoot=%%A
  27.  
  28. :: Strip off the fraction of seconds
  29. SET LastBoot=%LastBoot:~0,12%
  30.  
  31. :: Display result in requested output format
  32. :Display
  33. ECHO.%* | FIND /I "/Q" >NUL
  34. IF ERRORLEVEL 1 (
  35. 	ECHO Last boot time of %Node%: %LastBoot:~0,4%-%LastBoot:~4,2%-%LastBoot:~6,2% %LastBoot:~8,2%:%LastBoot:~10,2%
  36. ) ELSE (
  37. 	ECHO.%LastBoot%
  38. )
  39.  
  40. :: Transfer LastBoot variable to parent environment
  41. ENDLOCAL & SET LastBoot=%LastBoot%
  42.  
  43. :: Done
  44. GOTO:EOF
  45.  
  46.  
  47. :Syntax
  48. ECHO.
  49. ECHO LastBoot.bat,  Version 1.01 for Windows XP Professional and later
  50. ECHO Display last boot time for any WMI enabled computer
  51. ECHO.
  52. ECHO Usage:  LASTBOOT.BAT  [ computer_name ]  [ /Q ]
  53. ECHO.
  54. ECHO Where:  "computer_name"  is an optional remote computer name
  55. ECHO                          (default is the local computer name)
  56. ECHO         /Q               outputs date/time only, in YYYYMMDDhhmm format
  57. ECHO                          (default is message and DD-MM-YYYY, hh:mm format)
  58. ECHO.
  59. ECHO Notes:  Returns a string of 0s if the specified computer cannot be reached.
  60. ECHO         Requires WMIC.EXE, native as of Windows XP Professional.
  61. ECHO         The WMIC command line was generated using the WMI Code Generator,
  62. ECHO         http://www.robvanderwoude.com/wmigen.html
  63. ECHO.
  64. ECHO Written by Rob van der Woude
  65. ECHO http://www.robvanderwoude.com
  66.  
  67. ENDLOCAL
  68.  
  69. :: Set return code 1
  70. COLOR FF
  71.  

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