태그 보관물: batch-file

batch-file

ErrorLevel의 값이 업데이트되지 않는 이유는 무엇입니까? test for DeepFreeze ether not installed or

컴퓨터에 Deep Freeze가 설치되어 있는지 또는 안전하게 해제되었는지 확인하기 위해 일괄 적으로 서브 루틴을 작성하려고합니다. (모르는 사람들을 위해, 딥 프리즈 운영 체제에서 되돌리기 / 방지 및 디스크 변경에 일반적으로 사용되는 프로그램입니다.) dfc.exe 결정을 내리는 데 사용할 수있는 deep freeze 설치 파일입니다. 해동되면 0을 반환하고 고정 된 경우 1을 반환합니다.

기본적으로 컴퓨터가 정지 된 경우 특정 작업을 설치하는 스크립트를 실행하는 것이 의미가 없다고 생각했습니다. 먼저 dfc.exe가 설치되어 있는지 확인한 다음 해빙 / 고정 상태를 확인하려고 시도하지만 문제가있는 이유는 dfc의 반환 값이 errorlevel을 두 번째로 확인하는 위치를 업데이트하지 않는 것입니다.

누구도 왜 두 번째 ErrorLevel 검사의 반환 값을 볼 수 없는지 알 수 있습니다 ( on line 41 ) 아래 코드도 포함 시켰습니다.

편집 : 코드 앞에 내 사고 프로세스에 대한 의사 코드를 추가했습니다.

::Point1
IF Dfc.exe is present then (
    ::Point2
    If "dfc get /ISFROZEN" returns FROZEN then (
        ::Point3
        Write out to file so we can know that something was skipped,
        goto EOF to close out of script and avoid wasting cycles installing
    )
    ::Point4

::Deep freeze is installed, but thawed or it would have gotten caught in the "FROZEN" IF
::Fall through out of outer IF without running anything else
)
::Point5

GOTO (Return to where we left to check if we were wasting time with a label)

아래 코드

@ECHO Off
::CheckForDF here
ECHO We will now test for DeepFreeze ether not installed or is thawed
Pause
ECHO.

set flagfile=C:\testjava.txt
::Flagfile variable should already be defined before calling this function
Goto :CheckForDF
:DFNotFrozen
ECHO DeepFreeze wasn't installed or is currently thawed
ECHO **Continue with the rest of the script**
ECHO.
PAUSE

GOTO:eof







::***************************
::********Subroutines********
::***************************


:CheckForDF
WHERE dfc >nul 2>&1


::ErrorLEvel 0 means file was found, which means DF is installed

IF %ERRORLEVEL% EQU 0 (
   dfc get /ISFROZEN
   ECHO %errorlevel%
   ::ErrorLevel 0 - THAWED and ready to install
   ::ErrorLevel 1 - FROZEN and pointless to try

   IF %errorlevel% EQU 1 (

      ::Echo out what WOULD have been installed to a file so we could check that the
      ::   script still ran (and get an idea of how bad we need to unfreeze and log into each computer)
      ECHO %flagfile% %date%%time% >> C:\BRCCIT\ScriptsSkippedByDeepFreeze.txt
      ECHO ****DeepFreeze is currently frozen****

      ECHO.
      PAUSE
      ::Else - DeepFreeze is thawed. return to normal script
      goto :EOF

   )

   ::DeepFreeze is thawed, but is installed. We'll just fall through to the not installed result
)

::DeepFreeze Installation not found. Okay to return to normal script
ECHO DeepFreeze is not installed
goto :DFNotFrozen

업데이트 : 중첩 된 IF를 포기하고 GOTO 및 Labels로 돌아갔습니다. 그것은 꽤 아니지만,이 코드는 실제로 작동하고 문자 그대로 10 분과 같습니다. 인공 네 스팅 (nesting)의 시각적 효과를 내기 위해 들여 썼습니다.

@ECHO Off
::CheckForDF here
ECHO We will now test for DeepFreeze ether not installed or is thawed
Pause
ECHO.

set flagfile=C:\testjava.txt
::Flagfile variable should already be defined before calling this function
Goto :CheckForDF
:DFNotFrozen
ECHO DeepFreeze wasn't installed or is currently thawed
ECHO **Continue with the rest of the script**
ECHO.
PAUSE

GOTO:eof



::***************************
::********Subroutines********
::***************************



:CheckForDF
WHERE dfc >nul 2>&1

IF %ErrorLevel% EQU 0 Goto :DFInstalled
::ELSE - DF Not  found
GOTO :ReturnToScript


    :DFInstalled
    ::DFC.exe get /ISFROZEN
    Call ExitWithSpecifiedCode.bat 1
    ::If Frozen
    IF %ErrorLevel% EQU 1 GOTO DFFrozen
    ::If Thawed
    IF %ErrorLevel% EQU 0 GOTO ReturnToScript


        :DFFrozen
        ECHO %flagfile% %date%%time% >> C:\BRCCIT\ScriptsSkippedByDeepFreeze.txt
        ECHO ****DeepFreeze is currently frozen****

        ECHO.
        PAUSE
        ::Exit Script Execution
        goto :EOF

:ReturnToScript
ECHO ReturnToScript
PAUSE
GOTO (Return to script execution)



답변

ERRORLEVEL은! ERRORLEVEL을 사용하지 않으면 IF 문과 같은 컨트롤 블록을 업데이트하지 않습니다! 대신 % ERRORLEVEL % 대신 코드 시작 부분에 다음 명령을 사용하십시오.
setlocal ENABLEDELAYEDEXPANSION

만나다 http://batcheero.blogspot.ca/2007/06/how-to-enabledelayedexpansion.html


답변

문제는 두 번째 검사가 첫 번째 검사가 0 인 경우에만 사용되는 코드 블록 내에 있다는 것입니다. 다음과 같습니다.

IF (we want to go out) THEN (
  IF (it rains) THEN (
    take umbrella
  )
)

보세요, 비가 내리는 지 아닌지 확인하는 두 번째 점검은 우리가 나가기를 원한다면 수행됩니다. 우리가 나가고 싶지 않다면, 우리는 비를 확인하지 않아도됩니다.

해결책 : 두 번째 if에 도달하기 전에 첫 번째 IF 후 블록을 닫으십시오.

IF %ERRORLEVEL% EQU 0 (
  do stuff
)
IF %errorlevel% EQU 1 (
  do other stuff
)

또는 ELSE를 사용하십시오.

IF %ERRORLEVEL% EQU 0 (
  do stuff
)
ELSE (
  :: errorlevel was anything other than 0, i.e. NEQ 0
  do other stuff
)


답변

나는 쉽게 사용할 수있는 윈도우 머신을 가지고 있지 않지만, 그것이 나라면, 나는 ECHO 진술은 변하지 않는다. ERRORLEVEL.

아마도 이것은 관련이 없지만 어쩌면 대문자와 소문자를 처리하는 명령 인터프리터를 사용하고있을 것입니다 ERRORLEVEL 다르게? 그것이 아무 효과가 없더라도, 일관성이없는 것은 특이하다.


답변

다음 배치 파일은 찾고있는 상태를 제공합니다. 쉽게 조정할 수 있습니다. gotos 및 귀하의 필요에 맞는 라벨.

파일이 존재하는지 여부를 확인하고 ERRORLEVEL 적절하게 업데이트됩니다.

@echo off

REM ---------------------------------------------------------------------------------
REM -- Check the status of Deep Freeze
set "DFC=C:\Program Files (x86)\Faronics\Deep Freeze Enterprise\DFC.exe"
if not exist "%DFC%" goto alwaysRun
"%DFC%" get /ISFROZEN >nul 2>&1
if [9009] == [%ERRORLEVEL%] goto alwaysRun
if ERRORLEVEL 1 goto isFrozen
if ERRORLEVEL 0 goto isThawed
REM ---------------------------------------------------------------------------------





REM ---------------------------------------------------------------------------------
REM -- Run all the commands when the workstation is frozen
REM -- (these commands are skipped when the workstation is thawed)
goto alwaysRun
:isFrozen

echo The workstation is frozen
REM ---------------------------------------------------------------------------------





REM ---------------------------------------------------------------------------------
REM -- Run all the commands when the workstation is thawed
REM -- (these commands are skipped when the workstation is frozen)
goto alwaysRun
:isThawed

echo The workstation is thawed
REM ---------------------------------------------------------------------------------





REM ---------------------------------------------------------------------------------
:alwaysRun

echo Always run this part
REM ---------------------------------------------------------------------------------





REM ---------------------------------------------------------------------------------
:end
pause


답변