Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ckbkschd.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Keep variables local
  6. SETLOCAL
  7.  
  8. :: Check command line arguments -- only 1 allowed: a remote computer name
  9. IF NOT "%~2"=="" GOTO Syntax
  10. ECHO "%~1" | FINDSTR.EXE /R /I    /C:"[/?]" >NUL && GOTO Syntax
  11. ECHO "%~1" | FINDSTR.EXE /R /I /B /C:".-"   >NUL && GOTO Syntax
  12.  
  13. :: Check the first command line argument: optional remote
  14. :: computer name; if specified, PING it to check connectivity
  15. IF "%~1"=="" (
  16. 	SET Node=%ComputerName%
  17. ) ELSE (
  18. 	PING.EXE %~1 2>NUL | FIND.EXE "TTL=" >NUL
  19. 	IF ERRORLEVEL 1 (
  20. 		ECHO Unable to connect to %~1
  21. 		GOTO:EOF
  22. 	)
  23. 	SET Node=%~1
  24. )
  25.  
  26. :: Locate the directory where the Scheduled Tasks are located
  27. SET TaskDir=
  28. :: Try WMIC first
  29. FOR /F "tokens=2 delims==" %%A IN ('WMIC.EXE /Node:%Node% Path Win32_OperatingSystem Get WindowsDirectory /Format:List 2^>NUL') DO SET TaskDir=\\%Node%\%%A\Tasks
  30. :: If not found, try the default locations
  31. IF DEFINED TaskDir (
  32. 	SET TaskDir=%TaskDir::=$%
  33. ) ELSE (
  34. 	IF EXIST \\%Node%\C$\WINDOWS\Tasks SET TaskDir=\\%Node%\C$\WINDOWS\Tasks
  35. 	IF EXIST \\%Node%\C$\WINNT\Tasks   SET TaskDir=\\%Node%\C$\WINNT\Tasks
  36. )
  37. :: If still not found, abort the mission
  38. IF NOT DEFINED TaskDir (
  39. 	ECHO Unable to locate Scheduled Tasks folder on %Node%
  40. 	GOTO:EOF
  41. )
  42.  
  43. :: Check if JT.EXE is available and if not, offer to download it
  44. SET JTAvailable=
  45. SET Download=
  46. JT.EXE /? >NUL 2>&1
  47. IF ERRORLEVEL 1 (
  48. 	SET JTAvailable=No
  49. 	ECHO.>CON
  50. 	ECHO This batch file requires Microsoft's JT utility.>CON
  51. 	SET /P Download=Do you want to download it now? [y/N] >CON
  52. )
  53.  
  54. :: Start download if requested
  55. IF /I "%Download%"=="Y" (
  56. 	START "JT" "ftp://ftp.microsoft.com/reskit/win2000/jt.zip"
  57. 	ECHO.>CON
  58. 	ECHO Install the downloaded file and make sure JT.EXE is in the PATH.>CON
  59. 	ECHO Then try again.>CON
  60. )
  61.  
  62. :: Abort if JT.EXE is not available yet
  63. IF "%JTAvailable%"=="No" GOTO End
  64.  
  65. :: Check all Scheduled Tasks with the word "backup" in their names
  66. FOR %%A IN ("%TaskDir%\*backup*.job") DO CALL :CheckJob "%%~fA"
  67.  
  68. ENDLOCAL
  69. GOTO:EOF
  70.  
  71.  
  72. :CheckJob
  73. SETLOCAL
  74. SET ScheduledTask="%~1"
  75. FOR /F "tokens=1*" %%B IN ('JT.EXE /LJ %ScheduledTask% /PJ ^| FINDSTR.EXE /R /B /C:"  MostRecentRun:"') DO SET MostRecentRun=%%C
  76. FOR /F "tokens=1*" %%B IN ('JT.EXE /LJ %ScheduledTask% /PJ ^| FINDSTR.EXE /R /B /C:"  NextRun:"      ') DO SET NextRun=%%C
  77. FOR /F "tokens=1*" %%B IN ('JT.EXE /LJ %ScheduledTask% /PJ ^| FINDSTR.EXE /R /B /C:"  StartError:"   ') DO SET StartError=%%C
  78. FOR /F "tokens=1*" %%B IN ('JT.EXE /LJ %ScheduledTask% /PJ ^| FINDSTR.EXE /R /B /C:"  ExitCode:"     ') DO SET ExitCode=%%C
  79. SET ScheduledTask
  80. SET MostRecentRun
  81. SET NextRun
  82. SET StartError
  83. SET ExitCode
  84. ECHO.
  85. ENDLOCAL
  86. GOTO:EOF
  87.  
  88.  
  89. :Syntax
  90. ECHO.
  91. ECHO CkBkSchd.bat (ChecK BacKup SCHeDuled tasks),  Version 0.10 for Windows 2000/XP
  92. ECHO Display the status of all Scheduled Tasks with the word "backup" in their names
  93. ECHO.
  94. ECHO Usage:  CKBKSCHD.BAT  [ remote_computer ]
  95. ECHO.
  96. ECHO Output: ScheduledTask="\\remote_computer\C$\WINNT\Tasks\My daily backup.job"
  97. ECHO         MostRecentRun=06/25/2008 22:40:00
  98. ECHO         NextRun=06/26/2008 22:40:00
  99. ECHO         StartError=S_OK
  100. ECHO         ExitCode=0
  101. ECHO.
  102. ECHO Notes:  Requires Microsoft's JT, and will prompt you to download it from
  103. ECHO         ftp://ftp.microsoft.com/reskit/win2000/jt.zip if it isn't found.
  104. ECHO         This batch file will fail on remote non-XP computers that have
  105. ECHO         Windows installed in a non-default partition, i.e. multi-boot.
  106. ECHO         Modify the FOR command in line 66 to query other Scheduled Tasks.
  107. ECHO.
  108. ECHO Written by Rob van der Woude
  109. ECHO http://www.robvanderwoude.com
  110.  
  111. IF "%OS%"=="Windows_NT" ENDLOCAL
  112.  

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