【发布时间】:2014-02-23 07:35:51
【问题描述】:
已经很晚了,看着这个我累了。有人能解释一下我在这个脚本中哪里可能有错误的语法吗?
该脚本会查看computers.txt 文件中的活动机器上是否存在旧安装。如果是这样,它应该卸载它,复制新安装,然后安装它。如果有任何失败,请记录到其各自的日志文件。
@echo off
:CheckifLogsExist
if NOT exist Uninstall.log (
copy /y nul Uninstall.log
) else (
del Uninstall.log && copy /y nul Uninstall.log
)
if NOT exist WMIC.log (
copy /y nul WMIC.log
) else (
del WMIC.log && copy /y nul WMIC.log
)
if NOT exist Copying.log (
copy /y nul Copying.log
) else (
del Copying.log && copy /y nul Copying.log
)
if NOT exist Install.log (
copy /y nul Install.log
) else (
del Install.log && copy /y nul Install.log
)
:checkifalive
for /F %%I IN (computers.txt) DO
(
ping -n 1 %%I
if NOT %errorlevel%==0 echo Machine offline && goto:EOF
:Uninstall
echo "Uninstalling previous version of Symantec Endpoint Protection"
psexec \\%%I -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Uninstall.log
:copy
echo "Finding out which processor is in the machine"
wmic cpu list brief > temp.out
findstr /I "86" temp.out && goto copy86 || goto copy64
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> WMIC.log
:copy86
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install86
:copy64
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install64
:Install86
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Install64
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Finish
)
【问题讨论】:
-
很抱歉,太晚了,您很累,但如果您需要帮助,您需要提供更多信息。究竟是什么错误,在脚本中的哪一点?您是否尝试过使用
@echo on运行它以查看它在每一行上的作用? -
另外,我认为这不会导致语法错误,但会导致它不符合您的预期 - 我认为您不能像批量那样在 for 循环中使用 goto .对于实际安装部分,您可能会使用子例程做得更好。
-
@Vicky 是对的:
goto打破了循环。但是您还有另一个问题:您需要延迟扩展才能在块内使用变量。%errorlevel%不是,正如您所期望的,它与之前的for值相同,并且不会在(和)之间变化。
标签: batch-file if-statement for-loop command-prompt