How can I run my script when I’m sure it’s run as administered?

Asked

Viewed 1,177 times

3

I want to run a batch(.bat) and check if it was opened as an administrator, here’s an example of what I want on 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 don’t want to use this way because the administrator user can use another name.

runas /user:Administrator meubat.bat

2 - Checking the return of this command I can’t pick up anything that I can be sure I am as an administrator

whoami /priv

Running the command (whoami /priv):

inserir a descrição da imagem aqui

Running command(whoami /priv) as administrator:

inserir a descrição da imagem aqui

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

echo %USERDOMAIN%\%USERNAME%

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.