如何在批处理脚本中获取中央时间?

时间:2013-01-03 10:09:14

标签: batch-file

我必须基本上使用依赖于当前中心时间的if条件。我该如何调用当前的CT?

1 个答案:

答案 0 :(得分:0)

请参考this batch script在Windows计算机上使用不同的时区审核/添加/设置时间。为了便于访问,我发布在脚本下面:

@echo off
TITLE Check/Set Time Zone Configuration
COLOR 17

:OSCHK
FOR /F "tokens=4-5 delims=, " %%g IN ('wmic os get caption ^|find /I "Windows"') DO (IF /I "%%g"=="2003" (set OSVER=%%g) ELSE (IF /I "%%g"=="2008" (IF "%%g%%h"=="2008R2" (set OSVER=%%g%%h) ELSE set OSVER=%%g)))
FOR /F "tokens=3 delims= " %%g IN ('wmic os get caption ^|find /I "Windows"') DO (IF /I "%%g"=="7" set OSVER=%%g)

IF /I "%OSVER%"=="" (goto OSWARN) ELSE (goto TZRVW)

:OSWARN
echo.
echo.
echo *************************************
echo WARNING
echo *************************************
echo.
echo This script is designed to run on the following
echo Windows operating systems only:
echo Servers – 2003, 2008, 2008 R2
echo Clients – Windows 7
echo.
goto END

:TZRVW
cls
echo.
echo The time zone on this machine is:
FOR /F "skip=1 tokens=*" %%g IN ('wmic timezone get description ^|find " "') Do (@echo %%g)
goto TZQRY

:TZQRY
echo.
SET TZQRY=
SET /P TZQRY=Do you need to change the Time Zone? (y/n/q to quit): %=%
echo.
IF /I "%TZQRY%"=="y" (goto TZSEL) ELSE (IF /I "%TZQRY%"=="n" (goto END) ELSE (IF /I "%TZQRY%"=="q" (goto END)))
IF /I NOT "%TZQRY%"=="y" (IF /I NOT "%TZQRY%"=="n" (IF /I NOT "%TZQRY%"=="q" goto TZQRY0))

:TZQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto TZRVW

:TZSEL
cls
echo.
echo Which Time Zone should this machine have?
echo 1. Pacific Time
echo 2. Mountain Time
echo 3. Arizona
echo 4. Central Time
echo 5. Eastern Time
echo 6. Other

echo.
SET SYSTZ=
SET /P SYSTZ= %=%
IF /I "%SYSTZ%"=="1" (goto TZPAC) ELSE (IF /I "%SYSTZ%"=="2" (goto TZMTN) ELSE (IF /I "%SYSTZ%"=="3" (goto TZARZ) ELSE (IF /I "%SYSTZ%"=="4" (goto TZCEN) ELSE (IF /I "%SYSTZ%"=="5" (goto TZEAS) ELSE (IF /I "%SYSTZ%"=="6" (goto TZOTH))))))
IF /I NOT "%SYSTZ%"=="1" (IF /I NOT "%SYSTZ%"=="2" (IF /I NOT "%SYSTZ%"=="3" (IF /I NOT "%SYSTZ%"=="4" (IF /I NOT "%SYSTZ%"=="5" (IF /I NOT "%SYSTZ%"=="6" (goto TZSEL0))))))

:TZSEL0
echo.
echo You have made an invalid entry – please try again.
echo.
pause
goto TZSEL

:TZPAC
IF /I "%OSVER%"=="2003" (control timedate.cpl,,/Z Pacific Standard Time) ELSE (IF /I "%OSVER%"=="2008" (tzone -Zone Pacific Time) ELSE (IF /I "%OSVER%"=="2008R2" (tzutil /s "Pacific Standard Time") ELSE (IF /I "%OSVER%"=="7" (tzutil /s "Pacific Standard Time"))))
goto TZRVW

:TZMTN
IF /I "%OSVER%"=="2003" (control timedate.cpl,,/Z Mountain Standard Time) ELSE (IF /I "%OSVER%"=="2008" (tzone -Zone Mountain Time) ELSE (IF /I "%OSVER%"=="2008R2" (tzutil /s "Mountain Standard Time") ELSE (IF /I "%OSVER%"=="7" (tzutil /s "Mountain Standard Time"))))
goto TZRVW

:TZARZ
IF /I "%OSVER%"=="2003" (control timedate.cpl,,/Z US Mountain Standard Time) ELSE (IF /I "%OSVER%"=="2008" (tzone -Zone Arizona) ELSE (IF /I "%OSVER%"=="2008R2" (tzutil /s "US Mountain Standard Time") ELSE (IF /I "%OSVER%"=="7" (tzutil /s "US Mountain Standard Time"))))
goto TZRVW

:TZCEN
IF /I "%OSVER%"=="2003" (control timedate.cpl,,/Z Central Standard Time) ELSE (IF /I "%OSVER%"=="2008" (tzone -Zone "Central Time (US & Canada)") ELSE (IF /I "%OSVER%"=="2008R2" (tzutil /s "Central Standard Time") ELSE (IF /I "%OSVER%"=="7" (tzutil /s "Central Standard Time"))))
goto TZRVW

:TZEAS
IF /I "%OSVER%"=="2003" (control timedate.cpl,,/Z Eastern Standard Time) ELSE (IF /I "%OSVER%"=="2008" (tzone -Zone Eastern Time) ELSE (IF /I "%OSVER%"=="2008R2" (tzutil /s "Eastern Standard Time") ELSE (IF /I "%OSVER%"=="7" (tzutil /s "Eastern Standard Time"))))
goto TZRVW

:TZOTH
echo.
echo Control Panel will now open so you
echo can set the time zone manually.
start control timedate.cpl
echo.
pause
goto TZRVW

:END
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

希望它有所帮助!