0

I've been experiencing an issue, and I don't really know where is the problem or if I set a variable I shouldn't use. When I run this script with administrator rights it all goes well, but when I do not it simply should just show me the message at the bottom, but instead it goes to the variable of restart which is inside the if (true).

How can I fix it?

@echo off
goto check_Permissions

:check_Permissions
    ##Reddit: from here everything starts##
    echo Permisos de administrador requeridos. Detectando permisos...
    net session >nul 2>&1
    if %errorLevel% == 0 (
        echo Exito: Permisos de administrador confirmados.
            ##Reddit: if it detects admin rights it goes well##
            timeout 5
            :choice
            echo Some echo here
            echo NOTA: Si presionas Ctrl+C en una pausa ejecutara el codigo siguiente, para cerrarlo usa ALT+F4 o con el raton.
            echo Selecciona:
            echo [A] Escribir IP estatica
            echo [B] Seleccionar DHCP
            echo [C] Insertar sufijo DNS
            echo [D] Insertar nuevo nombre de equipo
            echo [E] Meter equipo en dominio
            echo [Y] Reiniciar
            echo [Z] Salir
            echo.

            SET /P C=[A,B,C,D,E,Y,Z]?
            for %%? in (A) do if /I "%C%"=="%%?" goto A
            for %%? in (B) do if /I "%C%"=="%%?" goto B
            for %%? in (C) do if /I "%C%"=="%%?" goto C
            for %%? in (D) do if /I "%C%"=="%%?" goto D
            for %%? in (E) do if /I "%C%"=="%%?" goto E

            for %%? in (Y) do if /I "%C%"=="%%?" goto Y
            for %%? in (Z) do if /I "%C%"=="%%?" goto Z
            goto choice

            ##Reddit: a lot of code goes here, not relevant##

            :Y
            echo ¿Quieres reiniciar el equipo? (S/N)
##Reddit: The variable thats causing me problems##
            set /P D=[S,N]?
            for %%? in (S) do if /I "%D%"=="%%?" goto shutdown
            for %%? in (N) do if /I "%D%"=="%%?" goto choice

            :shutdown
            shutdown -r -t 30
            echo Reiniciando el equipo en 30 segundos
            timeout 30 /NOBREAK

            :Z
            exit
            :end
    pause
    ) else (
        echo Fracaso: Permisos insuficientes.
##Reddit: if it reaches here because insufficient permissions this happens, (see image below)##
        echo EJECUTA ESTE SCRIPT COMO ADMINISTRADOR
    )

    pause >nul

This is the variable I commented before

Thurion
  • 1
  • 1

1 Answers1

0

I ended up doing it in another way, replacing the if else:
IF ELSE STATEMENT REMOVED:

goto check_Permissions

:check_Permissions
    echo Permisos de administrador requeridos. Detectando permisos...
    net session >nul 2>&1
    if %errorLevel% == 0 (
        echo Exito: Permisos de administrador confirmados.
        ##CODE HERE##
    ) else (
        echo Fracaso: Permisos insuficientes.
        echo EJECUTA ESTE SCRIPT COMO ADMINISTRADOR
    )

    pause >nul

New code version of https://stackoverflow.com/a/38856823/2193477

@echo off
net.exe session 1>NUL 2>NUL || goto :not_admin
echo SUCCEED
goto :eof

:not_admin
echo ERROR: Please run as administrator.
exit /b 1
##CODE HERE##

Thanks anyways.

Thurion
  • 1
  • 1
  • I recommend to replace `if %errorLevel% == 0 (` by `if not errorlevel 1 (` which means if exit code of `%SystemRoot%\System32\net.exe` (better specified in batch file with that fully qualified file name) is less than 1 which means equal 0 as `net.exe` does not exit with a negative value. That is the syntax to use as described by the usage help of command __IF__ output on running `if /?`. Best would be changing the order with `if errorlevel 1 (` and code for failure `) else (` and code for success and `)`. – Mofi Aug 11 '22 at 16:39
  • See also [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/) and [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) The latter explains in full details why `set /P` should not be used for a choice menu, but the command __CHOICE__. My answer on [Can't run as Admin](https://stackoverflow.com/a/41493926/3074564) could be useful for you too. – Mofi Aug 11 '22 at 16:40