How can I run my script making sure it was run as managed?

3

I want to run a batch (.bat) and check if it was opened as an administrator, here is an example of what I want in linux:

if [ 'whoami' == 'root' ] then
    echo I am root
else
    echo I am not root
fi

I searched but only found these methods:

1 - I do not want to use this way because the admin user can use another name.

runas /user:Administrator meubat.bat

2 - Checking the return of this command I can not get anything that can make sure I'm an administrator

whoami /priv

Running the command (whoami / priv):

Runningthe(whoami/priv)commandasanadmin:

This command shows the same user name running as managed or not.

echo %USERDOMAIN%\%USERNAME%

How can I run my script making sure it was run as managed?

    
asked by anonymous 13.09.2017 / 23:03

1 answer

2

This is currently the best way:

@echo off
echo Detectando privilegios...
net session >nul 2>&1
if %errorLevel% == 0 (
    rem //Comandos com privilegios de Administrador
    echo Privilegios de Administrador confirmados.
) else (
    rem //Comandos sem privilegios de Administrador
    echo Sem privilegios de Administrador.
)

pause >nul

Works with:

  • Windows XP
  • Windows Vista
  • Windows 7
  • Windows 8
  • Windows 10
13.09.2017 / 23:17