Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pmsoon.bat

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

  1. @ECHO OFF
  2. :: Keep all variables local
  3. SETLOCAL
  4. :: PMSOON Version
  5. SET PMSoonVer=1.01
  6.  
  7. :: Check command line parameters.
  8. :: Is a command specified?
  9. IF [%2]==[] GOTO Syntax
  10. :: No illegal switches used?
  11. ECHO.%* | FIND /I "/Every" >NUL
  12. IF NOT ERRORLEVEL 1 GOTO Syntax
  13. ECHO.%* | FIND /I "/Next" >NUL
  14. IF NOT ERRORLEVEL 1 GOTO Syntax
  15. :: Is the first parameter a valid system name?
  16. SET RemotePC=%1
  17. IF "%RemotePC:~0,2%"=="\\" SET RemotePC=%RemotePC:~2%
  18. PING %RemotePC% 2>NUL | FIND "TTL=" >NUL
  19. IF ERRORLEVEL 1 GOTO Syntax
  20. :: Parse the remote command from the batch file's command line.
  21. :: Replace ("%*") by ('ECHO.%*') for NT 4 without SP.
  22. FOR /F "tokens=1* delims= " %%A IN ("%*") DO SET PMSoonCommand=%%B
  23.  
  24. :: Check for time differences between local and remote system.
  25. FOR /F "tokens=6* delims= " %%A IN ('NET TIME \\%ComputerName% ^| FIND "Current"') DO (
  26. 	SET LocalDate=%%A
  27. 	SET LocalTime=%%B
  28. )
  29. FOR /F "tokens=6* delims= " %%A IN ('NET TIME \\%RemotePC% ^| FIND "Current"') DO (
  30. 	SET RemoteDate=%%A
  31. 	SET RemoteTime=%%B
  32. )
  33. :: Don't use this batch file 1 minute from midnight, as
  34. :: the following date comparison will most likely fail.
  35. IF NOT "%LocalDate%"=="%RemoteDate%" GOTO ErrDate
  36. SET LocalHH=%LocalTime:~0,2%
  37. SET Localmm=%LocalTime:~3,2%
  38. SET RemoteHH=%RemoteTime:~0,2%
  39. SET Remotemm=%RemoteTime:~3,2%
  40. :: If both date and time match, we're in luck.
  41. IF "%LocalTime%"=="%RemoteTime%" GOTO InSync
  42. :: Get an absolute value for the time difference by squaring it
  43. SET /A Diffmm   = %Localmm% - %Remotemm%
  44. SET /A DiffmmSq = %Diffmm%  * %Diffmm%
  45. :: If the hours match and the difference in minutes is 1
  46. :: minute, consider the time of the systems synchronized.
  47. IF "%LocalHH%"=="%RemoteHH%" IF %DiffmmSq% LSS 2 GOTO InSync
  48. :: If the hours do not match, check if the
  49. :: time difference is still 1 minute only.
  50. SET /A DiffHH   = %LocalHH% - %RemoteHH%
  51. SET /A DiffHHSq = %DiffHH%  * %DiffHH%
  52. :: Check for 1 minute difference on the whole hour, as with 2:00 and 1:59.
  53. IF %DiffHHSq% LSS 2 IF %DiffmmSq% EQU 3481 GOTO InSync
  54. :: The systems' time difference seems to be too large for this batch file.
  55. GOTO ErrSync
  56.  
  57. :: If you manage to get here, the systems seem to be synchronized close enough.
  58. :InSync
  59. :: Calculte time 2 minutes from now
  60. SET /A NearFuturemm = %Remotemm% + 2
  61. SET /A NearFutureHH = %RemoteHH%
  62. IF %NearFuturemm% GTR 59 (
  63. 	SET /A NearFuturemm = %NearFuturemm% - 60
  64. 	SET /A NearFutureHH = %NearFutureHH% + 1
  65. )
  66. IF %NearFuturemm% GTR 59 IF %NearFutureHH% GTR 23 SET /A NearFutureHH = %NearFutureHH% - 24
  67. IF %NearFuturemm% LSS 10 SET NearFuturemm=0%NearFuturemm% 
  68. SET NearFuture=%NearFutureHH%:%NearFuturemm%
  69. :: Finaly, schedule the command
  70. AT \\%RemotePC% %NearFuture% %PMSoonCommand%
  71. :: And check the result
  72. AT \\%RemotePC%
  73. GOTO End
  74.  
  75.  
  76. :ErrHdr1
  77. (ECHO.)1>&2
  78. (ECHO PMSOON,  Version %PMSoonVer% for Windows NT)1>&2
  79. (ECHO Poor Man's replacement for the NT 4 Resource Kit's SOON command.)1>&2
  80. GOTO:EOF
  81.  
  82.  
  83. :ErrHdr2
  84. (ECHO.)1>&2
  85. (ECHO Written by Rob van der Woude)1>&2
  86. (ECHO http://www.robvanderwoude.com)1>&2
  87. (ECHO.)1>&2
  88. GOTO:EOF
  89.  
  90.  
  91. :ErrTimeDisp
  92. NET TIME \\%ComputerName% | FIND "Current" 1>&2
  93. NET TIME \\%RemotePC%     | FIND "Current" 1>&2
  94. (ECHO.)1>&2
  95. GOTO:EOF
  96.  
  97.  
  98. :ErrFtr1
  99. (ECHO Command aborted.)1>&2
  100. (ECHO.)1>&2
  101. (ECHO Correct the problem ^(try NET TIME^) and try again.)1>&2
  102. GOTO:EOF
  103.  
  104.  
  105. :ErrDate
  106. CALL :ErrHdr1
  107. CALL :ErrHdr2
  108. (ECHO ERROR: The date settings on the local and remote system do not match.)1>&2
  109. (ECHO.)1>&2
  110. CALL :ErrTimeDisp
  111. CALL :ErrFtr1
  112. GOTO End
  113.  
  114.  
  115. :ErrSync
  116. CALL :ErrHdr1
  117. CALL :ErrHdr2
  118. (ECHO ERROR: The local and remote system's time settings differ more than 1 minute.)1>&2
  119. (ECHO.)1>&2
  120. CALL :ErrTimeDisp
  121. (ECHO Since PMSoon uses a 2 minute time interval, this is too risky.)1>&2
  122. (ECHO Your command might be scheduled to run 24 hours later than intended.)1>&2
  123. CALL :ErrFtr1
  124. GOTO End
  125.  
  126.  
  127. :Syntax
  128. CALL :ErrHdr1
  129. (ECHO Will schedule a command 2 minutes from now on a remote system.)1>&2
  130. (ECHO Scheduler service must be active on the remote system.)1>&2
  131. (ECHO You need to have administrative rights on the remote system.)1>&2
  132. (ECHO Though PMSOON uses AT to schedule the command, AT's /EVERY and)1>&2
  133. (ECHO /NEXT switches are NOT allowed.)1>&2
  134. CALL :ErrHdr2
  135. (ECHO Usage:  PMSOON  \\remote_pc  command2BXecuted)1>&2
  136.  
  137.  
  138. :End
  139. ENDLOCAL
  140.  

page last uploaded: 2017-08-21, 14:26