我寫了一個腳本,由Task Scheduler每天晚上23:00執行,讓NAS01在NAS02上電,如果它尚未打開,執行GoodSync作業備份NAS01,然後關閉NAS02完成後。該劇本並不漂亮,但它完成了這項工作。我有的問題是時間戳。我設置變量來設置日期和時間的格式,但是,這隻會引用整個腳本的變量的時間戳。我如何保持格式化,但能夠在每個被調用的地方獲得日期和時間?批處理文件 - 格式日誌記錄的日期/時間
正如您在腳本中看到的那樣,日誌文件名有日期/時間格式,日誌文件中記錄日期/時間格式。在我的格式會發生什麼:
- 強制24H時間
- 領先於時空被替換爲「0」(即「6:30」變爲「06:30」)
- 時間fomatting是HHMM的文件名和HH:MM:SS用於記錄
- 日期格式是文件名和YYYY/MM/DD YYYYMMDD用於記錄
腳本:
@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
謝謝!這正是我所需要的,我很欣賞關於「回聲」的頭腦。我測試並證實,這是可行的。 – eptesicus