I have a batch script made in notepad. It works, but there’s 2 segments of the script that make it close the cmd, and I just can’t understand why.
Here’s the script
@echo off
setlocal enabledelayedexpansion
title JC-ServerManager v1.0.0
set "database=admins.txt"
set "try_file=tryvalue.txt"
:: Leitura do valor de 'try' a partir do ficheiro 'tryvalue.txt'
if exist "%try_file%" (
set /p try=<"%try_file%"
) else (
set /a try=3
echo 3 >"%try_file%"
)
:START
cls
powershell -Command Write-Host -NoNewLine -ForegroundColor Yellow " ----------------------------- Server Manager v1.0.0 ----------------------------- " & echo.
:: Verifica se o utilizador está bloqueado
if "%try%" equ "blocked" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Your account is blocked. Please contact server admin to recover your permission." & echo.
echo Contact:
echo ...Email: [email protected]
echo ...Phone: +351 967 041 910
echo. & pause
exit /b
)
echo. & echo LOGIN: & echo.
set /p "user=...User: "
set /p "password=...Password: "
echo. & echo Checking data...
set "authenticated=false"
set "user_found=false"
set "user_status=OK"
set "user_permission=DEFAULT"
:: Verifica se o nome de usuário e a senha não estão vazios
if "%user%"=="" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "User cannot be empty" & echo.
echo. & pause
goto :START
)
if "%password%"=="" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Password cannot be empty" & echo.
echo. & pause
goto :START
)
:: Verifica credenciais do usuário, status e permissões
for /f "tokens=1,2,3,4,5 delims=:" %%a in ('type "%database%"') do (
if "%user%" equ "%%b" (
set "user_found=true"
if "%password%" equ "%%c" (
if "%%d" equ "BLOCKED" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Your account is blocked. Please contact server admin to recover your permission." & echo.
echo Contact:
echo ...Email: [email protected]
echo ...Phone: +351 967 041 910
echo. & pause
exit /b
) else (
set "authenticated=true"
set "user_permission=%%e"
goto :AUTHENTICATED
)
)
)
)
:: Verifica se o usuário foi encontrado mas a senha estava incorreta
if "!user_found!"=="true" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Incorrect password" & echo.
set /a try-=1
if !try! equ 0 (
:: Atualiza o status do usuário para BLOCKED
(for /f "tokens=1,2,3,4,5 delims=:" %%a in ('type "%database%"') do (
if "%%b" equ "%user%" (
echo %%a:%%b:%%c:BLOCKED:%%e
) else (
echo %%a:%%b:%%c:%%d:%%e
)
)) > "%database%"
echo blocked >"%try_file%"
) else (
echo !try! >"%try_file%"
)
echo. & pause
goto :START
)
:: Verifica se as credenciais estavam incorretas e o usuário não foi encontrado
if "!authenticated!"=="false" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "User not found or incorrect password" & echo.
echo. & pause
goto :START
)
:AUTHENTICATED
powershell -Command Write-Host -NoNewLine -ForegroundColor Green "You have been successfully authenticated!" & echo. & pause
:: Reset do contador de tentativas após autenticação bem-sucedida
set /a try=3
echo 3 >"%try_file%"
goto :OPTIONS
:OPTIONS
cls
powershell -Command Write-Host -NoNewLine -ForegroundColor Yellow " ----------------------------- Server Manager v1.0.0 ----------------------------- " & echo.
echo. & echo OPTIONS:
echo.
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[1] Profile" & echo.
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[2] Server Stats" & echo.
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[3] Connect to the server" & echo.
:: Exibe a opção de administrador apenas se a permissão for ADMIN
if "%user_permission%"=="ADMIN" (
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[4] Admin - Manager" & echo.
)
echo. & echo.
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[9] Logout" & echo.
powershell -Command Write-Host -NoNewLine -ForegroundColor White "[0] Exit" & echo.
echo.
set /p "aws1=^"
if "%aws1%"=="1" goto :PROFILE
if "%aws1%"=="2" goto :STATS
if "%aws1%"=="3" goto :CONNECT
:: Verifica a permissão para a opção admin
if "%aws1%"=="4" if "%user_permission%"=="ADMIN" goto :ADMIN
if "%aws1%"=="9" goto :START
if "%aws1%"=="0" (
exit /b
) else goto :AWSERROR
:PROFILE
cls
echo profile
pause
goto :OPTIONS
:STATS
cls
echo stats
pause
goto :OPTIONS
:CONNECT
cls
echo connect
pause
goto :OPTIONS
:ADMIN
cls
echo admin
pause
goto :OPTIONS
:AWSERROR
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Error: Invalid option. Please choose one from the option menu." & echo. & pause
cls
goto :OPTIONS
endlocal
exit /b
The 2 segments causing problems to the script are:
:: Verifica se o usuário foi encontrado mas a senha estava incorreta
if "!user_found!"=="true" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "Incorrect password" & echo.
set /a try-=1
if !try! equ 0 (
:: Atualiza o status do usuário para BLOCKED
(for /f "tokens=1,2,3,4,5 delims=:" %%a in ('type "%database%"') do (
if "%%b" equ "%user%" (
echo %%a:%%b:%%c:BLOCKED:%%e
) else (
echo %%a:%%b:%%c:%%d:%%e
)
)) > "%database%"
echo blocked >"%try_file%"
) else (
echo !try! >"%try_file%"
)
echo. & pause
goto :START
)
and
:: Verifica se as credenciais estavam incorretas e o usuário não foi encontrado
if "!authenticated!"=="false" (
powershell -Command Write-Host -NoNewLine -ForegroundColor Red "User not found or incorrect password" & echo.
echo. & pause
goto :START
)
They should show an error message, just like every other option, but instead, if the user or password are wrong, the cmd just closes. If they’re empty or correct it works, even the blocked status accounts works. The only issue is with the incorrect credentials.
Btw I am using 2 .txt files to run the script:
tryvalue.txt
3
and admins.txt
1:user1:password1:OK:ADMIN
2:user2:password2:OK:DEFAULT
3:user3:password3:BLOCKED:DEFAULT
I’ve tried to use an older version of the script and it works, but not with all the options I want it to be able to do.