我写了一个脚本,每天晚上23:00由任务计划程序执行,如果NAS02没有打开NAS01电源,执行GoodSync作业备份NAS01,并关闭NAS02完成。这个剧本并不漂亮,但是它完成了这项工作。我遇到的问题是时间戳。我设置变量来设置日期和时间的格式,但是,这只会引用整个脚本的变量的时间戳。如何保留格式,但能够在其被称为的每个点获取日期和时间?
从脚本中可以看出,日志文件名的日期/时间格式以及日志文件中日志记录的日期/时间格式。我的格式化会发生什么:
脚本:
@ECHO off
REM Change CMD window color and size.
color 37
mode 180,50
REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
SET HH=%time:~-11,2%
SET MM=%time:~-8,2%
SET SS=%time:~-5,2%
SET NAMETIMESTR=%HH: =0%%MM%
SET LOGTIMESTR=%HH: =0%:%MM%:%SS%
SET MYDATE=%DATE:~4,10%
SET NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%
SET LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt
:START
REM ### Checks IF 7xNAS02 is online. IF not, power it on.
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO %LOGDATESTR% %LOGTIMESTR% EXECUTING GSYNC SCRIPT >> %LOGFILE%
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO.
ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
ECHO and then execute the "STORAGE" backup job in GoodSync.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Checking power status of 7xNAS02... >> %LOGFILE%
ECHO Checking power status of 7xNAS02...
ECHO.
ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON
:ALREADY_ON
REM Sets variable to dictate that the server was already on before executing the script.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is already powered on and available. >> %LOGFILE%
ECHO 7xNAS02 is already powered on and available.
ECHO.
SET previously_off=1
GOTO EXECUTE_BACKUP
:POWER_ON
REM ### Powers on 7xNAS02
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is offline. Powering on now. >> %LOGFILE%
ECHO 7xNAS02 is offline. Powering on now.
ECHO.
SET previously_off=0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
ECHO %LOGDATESTR% %LOGTIMESTR% Pinging 7xNAS02 until it's available... >> %LOGFILE%
ECHO Pinging 7xNAS02 until it's available...
ECHO.
GOTO CHECK_IF_READY
:CHECK_IF_READY
REM ### Continually pings 7xNAS02 until it is online.
ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP
:EXECUTE_BACKUP
REM ### Executes GoodSync backup job, "STORAGE."
ECHO %LOGDATESTR% %LOGTIMESTR% Executing "STORAGE" backup job. >> %LOGFILE%
ECHO Executing "STORAGE" backup job.
ECHO.
C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
IF %previously_off% == 0 (
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
ECHO Sync job done. Shutting down 7xNAS02...
shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
GOTO SHUTDOWNCHECK
)
IF %previously_off% == 1 (
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
ECHO Sync job done. 7xNAS02 will stay powered on.
ECHO.
)
GOTO END
:SHUTDOWNCHECK
ping -n 5 %ipaddress1% >nul
IF ERRORLEVEL 1 (
REM ### 7xNAS02 has powered off.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 has powered off. >> %LOGFILE%
ECHO 7xNAS02 has powered off.
GOTO END
)
REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
ECHO ...
GOTO SHUTDOWNCHECK
:END
REM pause
ECHO %LOGDATESTR% %LOGTIMESTR% END OF SCRIPT >> %LOGFILE%
exit
编辑:感谢@Mofi,更新后的工作脚本如下。
@ECHO off
REM Change CMD window color and size.
color 37
mode 180,50
REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
CALL :GETDATETIME
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt
:START
REM ### Checks IF 7xNAS02 is online. IF not, power it on.
ECHO ################################################################################################################################ >> %LOGFILE%
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% EXECUTING GSYNC SCRIPT >> %LOGFILE%
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO/
ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
ECHO and then execute the "STORAGE" backup job in GoodSync.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Checking power status of 7xNAS02... >> %LOGFILE%
ECHO Checking power status of 7xNAS02...
ECHO/
ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON
:ALREADY_ON
REM Sets variable to dictate that the server was already on before executing the script.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is already powered on and available. >> %LOGFILE%
ECHO 7xNAS02 is already powered on and available.
ECHO/
SET previously_off=1
GOTO EXECUTE_BACKUP
:POWER_ON
REM ### Powers on 7xNAS02
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is offline. Powering on now. >> %LOGFILE%
ECHO 7xNAS02 is offline. Powering on now.
ECHO/
SET previously_off=0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Pinging 7xNAS02 until it's available... >> %LOGFILE%
ECHO Pinging 7xNAS02 until it's available...
ECHO/
GOTO CHECK_IF_READY
:CHECK_IF_READY
REM ### Continually pings 7xNAS02 until it is online.
ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP
:EXECUTE_BACKUP
REM ### Executes GoodSync backup job, "STORAGE."
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Executing "STORAGE" backup job. >> %LOGFILE%
ECHO Executing "STORAGE" backup job.
ECHO/
C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
IF %previously_off% == 0 (
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
ECHO Sync job done. Shutting down 7xNAS02...
shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
GOTO SHUTDOWNCHECK
)
IF %previously_off% == 1 (
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
ECHO Sync job done. 7xNAS02 will stay powered on.
ECHO/
)
GOTO END
:SHUTDOWNCHECK
ping -n 5 %ipaddress1% >nul
IF ERRORLEVEL 1 (
REM ### 7xNAS02 has powered off.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 has powered off. >> %LOGFILE%
ECHO 7xNAS02 has powered off.
GOTO END
)
REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
ECHO ...
GOTO SHUTDOWNCHECK
:END
REM pause
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% END OF SCRIPT >> %LOGFILE%
exit
:GETDATETIME
SET "HH=%time:~-11,2%"
SET "MM=%time:~-8,2%"
SET "SS=%time:~-5,2%"
SET "NAMETIMESTR=%HH: =0%%MM%"
SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
SET "MYDATE=%DATE:~4,10%"
SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
GOTO :EOF
答案 0 :(得分:0)
使用区域相关但访问速度更快的DATE
和TIME
环境变量的简单解决方案是使用子例程。
在最后一个命令exit
之后追加到批处理文件的末尾:
:GetDateTime
SET "HH=%time:~-11,2%"
SET "MM=%time:~-8,2%"
SET "SS=%time:~-5,2%"
SET "NAMETIMESTR=%HH: =0%%MM%"
SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
SET "MYDATE=%DATE:~4,10%"
SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
GOTO :EOF
这是子程序GetDateTime
。并删除批处理文件顶部的行,这些行也设置了这些环境变量。
使用LOGDATESTR
或LOGTIMESTR
在每一行上方插入一行:
CALL :GetDateTime
在命令提示符窗口call /?
中运行,以获取有关此方法的更多信息,以便在批处理文件中嵌入批处理文件并将其作为子例程调用。
还有一个提示:建议使用echo/
代替echo.
,请参阅DosTips论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/