mardi 22 novembre 2016

Conditional If Statement Syntax in Batch

I have checked several resources on conditional if statement syntax for batch scripts, and despite this, I'm unable to find my mistake. I typically avoid asking questions here that should be this simple, but it's the last task on a huge project so I'm hoping someone can help me see what I'm doing wrong. This is a script for a university project, and is merely for an exploration of concepts.

@Echo OFF

REM Check time remaining
for /f "tokens=3 delims=: " %%a in (
'cscript //nologo "%systemroot%\system32\slmgr.vbs" /dli ^| find "Time remaining: "' 
) do set "timeRemainingStatus=%%a"

Echo %timeRemainingStatus%
pause

REM Check SkipRearm dword value
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform"
set VALUE_NAME=SkipRearm

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)

Echo Value Name: %ValueName%
Echo Value Value: %ValueValue%
pause

IF %timeRemainingStatus% EQU 20160 (
Echo First level
pause
IF defined ValueName (
    Echo Second Level
    pause
    IF %ValueValue% == "0x0" (
        Echo Third Level
        pause
        regedit.exe /s "C:\Windows\SR.reg"
    )
) ELSE (
    Echo Fourth Level
    pause
    SLMGR /REARM
    )
)
pause

I have the echos and pauses in there so I can see what code runs and to verify that the variables are retaining their values. It crashes for me after the second pause (after the Echo Value Value: %ValueValue% command). I assume it's something wrong with syntax, but I've been unable to see any errors because the command prompt closes, despite all the pauses I have.

Aucun commentaire:

Enregistrer un commentaire