Search code examples
batch-fileencryptionvarbitlocker

Bitlocker conversion status as var in bat


So I am writing a script that should be able to decrypt bitlocker encrypted drives, show the percentage and conversion status refreshing those 2 every few seconds. The problem is that when it refreshes, it thinks the task is done and moves to the next step.

The script needs to be cmd due to the nature of the environment.

Thank you in advance for the insight and/or assist!

Here is the section of code giving me grief:

:statusloop
cls
set done=
set done=Conversion Status:    Fully Decrypted
for /f "tokens=*" %%i in ('manage-bde -status C: ^| findstr /C:"Conversion"') do ^set status1=%%i
for /f "tokens=*" %%i in ('manage-bde -status C: ^| findstr /C:"Percent"') do ^set STATUS=%%i
echo %status1%
echo %status%
ping 127.0.0.1>nul
if "%status1%" == "%done%" goto decryptedreboot 
    else goto statusloop

For those who need everything, here is all the code:

@echo OFF

goto check_Permissions

:check_Permissions
    echo Administrative permissions required. Detecting permissions...

    net session >nul 2>&1
    if %errorLevel% == 0 (
        echo Success: Administrative permissions confirmed.
        echo Standby for launch...
        ping 127.0.0.1>nul
        goto checker
    ) else (
        color C
        echo Failure: Current permissions inadequate.
        echo Please run as Admin and try again.
        ping 127.0.0.1>nul
        exit
    )

:checker
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.3" goto nope
if "%version%" == "6.2" goto nope
if "%version%" == "6.1" goto nope
if "%version%" == "6.0" goto nope
if "%version%" == "10.0" goto begin
rem etc etc
endlocal
goto begin

::Recovery Key - Paste or type in the recovery key between the quotes.
set rpin="" 

::No Comment
:begin

color A
cls
color 7
echo -----------------------------------------------
echo                Bitlocker Toolkit
echo -----------------------------------------------
echo.
echo How may I assist you today?
echo. 
echo 1) Get Recovery Key ID and 48 number recovery pin
echo 2) Change Bitlocker Pin
echo 3) Unlock the drive
echo 4) Decrypt this machine
echo 5) Oops! Wrong file, I need to exit! 
echo.
echo.
echo.
set ask=
set /p ask=I pick number:
if '%ask%'=='1' goto keys
if '%ask%'=='2' goto changepin
if '%ask%'=='3' goto unlock
if '%ask%'=='4' goto decrypt
if '%ask%'=='5' goto goodbye


:keys

cls
manage-bde -protectors C: -get

set ask=
set/p ask=Export to desktop? (Y/N):
if '%ask%'=='Y' goto export
if '%ask%'=='y' goto export
if '%ask%'=='N' goto begin
if '%ask%'=='n' goto begin

:export
cls
manage-bde -protectors C: -get>%userprofile%\Desktop\Bltiocker_recovery_key.txt
echo Exporting to Bitlocker_recovery_key.txt on Desktop\Bltiocker_recovery_key
ping 127.0.0.1>nul
goto begin

:changepin

manage-bde -changepin C:

pause

goto begin

:unlock
cls
IF "%rpin%"=="" set /p rpin=Please enter the recovery key including dashes:
manage-bde -unlock C: -recoverypassword %rpin%
pause
goto begin

:decrypt
cls
echo Last chance to turn back! Are you sure you want to decrypt
echo this machine?
echo.
echo.
set ask=
set/p ask=Are you sure? (Y/N):
if '%ask%'=='Y' goto begindecryptreboot
if '%ask%'=='y' goto begindecryptreboot
if '%ask%'=='N' goto begin
if '%ask%'=='n' goto begin

:begindecryptreboot
cls
IF "%rpin%"=="" set /p rpin=Please enter the recovery key including dashes:
manage-bde -unlock C: -recoverypassword %rpin%
ping 127.0.0.1>nul
manage-bde -off C:
cls
goto statusloop

:statusloop
cls
set done=
set done=Conversion Status:    Fully Decrypted
for /f "tokens=*" %%i in ('manage-bde -status C: ^| findstr /C:"Conversion"') do ^set status1=%%i
for /f "tokens=*" %%i in ('manage-bde -status C: ^| findstr /C:"Percent"') do ^set STATUS=%%i
echo %status1%
echo %status%
ping 127.0.0.1>nul
if "%status1%" == "%done%" goto decryptedreboot 
    else goto statusloop

:decryptedreboot
cls
echo Decryption complete. This PC will reboot in 5 minutes...
shutdown /f /r /t 300
echo Press any key to abort the reboot.
pause>nul
shutdown /a
cls
echo The shutdown has been aborted, returning to main menu...
timeout /t 5>nul
goto begin

::XP & 7 detection
:Nope
cls
color c
echo ***FATAL ERROR***
echo.
echo.
echo THIS SOLUTION IS DESIGNED ONLY FOR WINDOWS 10 COMPUTERS. YOU SHALL NOT PASS!
pause >nul
exit

::Goodbye, cruel world!
:goodbye
cls
color 3
echo Thank you for using the Bitlocker Toolkit.
echo.
echo.
echo.
echo So long and thanks for all the fish!
echo.
echo.
echo.
timeout /t 05 
exit /b

Solution

  • in your line

      else goto statusloop
    

    remove the else. You don't need it and because of your wrong if syntax it just generates an errormessage and continues with the next line of code, which is your shutdown code.

    Correct if syntax would be (in case you really want to keep the else):

    if "%status1%" == "%done%" ( 
      goto decryptedreboot 
    ) else ( 
      goto statusloop
    )
    

    Note: ) else ( has to be on the same physical line (and the spaces are critical)