Page 1 of 1

How to check for admin rights ?

Posted: 12 Jul 2014 07:25
by Dragokas
How to do it correctly ?

Admin rights - it's mean when user prompts a UAC message and press OK button.

Re: How to check for admin rights ?

Posted: 12 Jul 2014 10:00
by ShadowThief

Code: Select all

at


If you don't have admin rights, %errorlevel% will be 1. If you do have admin rights, %errorlevel% will be 0.

Re: How to check for admin rights ?

Posted: 12 Jul 2014 11:14
by Dragokas
Ok. But what command you suggest to execute ?

Re: How to check for admin rights ?

Posted: 12 Jul 2014 11:31
by foxidrive
Dragokas wrote:How to do it correctly ?

Admin rights - it's mean when user prompts a UAC message and press OK button.



What are you trying to do?

A user with admin permissions still has to elevate some tasks, and doesn't get a UAC message if UAC is disabled.

Re: How to check for admin rights ?

Posted: 12 Jul 2014 13:16
by Dragokas
Ok. My code must be run with admin privileges.
I have a function in my code to prompt user for an elevation with UAC message.

But there are two ways to run my.cmd:

1) Double click
2) Right Click -> run with admin privileges

In the second case i do not need to call Elevate function.
That's why first I must to check whether my code runned with admin privileges already.

I can't check it with file commands like echo.>"%SystemRoot%\Evelate_test"
or registry commands like reg add hklm\software\elevate_test
because there is little chance that the rights for a system folder or hklm hive will be broken (example - with virus infection).

I need a reliable check that will be work on any win-systems (Vista and above).

Re: How to check for admin rights ?

Posted: 12 Jul 2014 13:20
by ShadowThief
Dragokas wrote:Ok. But what command you suggest to execute ?

The command is literally called

Code: Select all

at


And then

Code: Select all

if %errorlevel%==1(
    echo Not admin
) else (
    echo admin
)

Re: How to check for admin rights ?

Posted: 12 Jul 2014 13:43
by Dragokas
not suitable.

Code: Select all

C:\Windows\system32>at
The AT command has been deprecated. Please use schtasks.exe instead.
The request is not supported.

C:\Windows\system32>echo %errorlevel%
1

C:\Windows\system32>ver
Microsoft Windows [Version 6.3.9600]

Re: How to check for admin rights ?

Posted: 12 Jul 2014 13:57
by Compo
Try this!

Code: Select all

@Echo Off
Net Session >Nul 2>&1||Call :ElevateFunction
Rem Already Elevated code Here
<SNIP />
Exit/B
:ElevateFunction
<SNIP />

Re: How to check for admin rights ?

Posted: 12 Jul 2014 14:08
by Dragokas
Thanks, Compo.
It works great.

Re: How to check for admin rights ?

Posted: 12 Jul 2014 14:13
by ShadowThief
Sorry, I didn't realize you wanted me to straight-up GIVE you the code.

Code: Select all

@echo off
at>nul

if %errorlevel%==1 (
   Call :ElevateFunction
)

<rest of your code goes here>

Re: How to check for admin rights ?

Posted: 12 Jul 2014 14:16
by Dragokas
Sorry, but i don't understand, what do you talking about.
Bad english.

Re: How to check for admin rights ?

Posted: 12 Jul 2014 14:22
by ShadowThief
Oh yeah, they got rid of at in Windows 8, didn't they... I wish you had mentioned that that's what you were running; I wouldn't have suggested it in the first place.

Re: How to check for admin rights ?

Posted: 18 Jul 2014 05:28
by Dos_Probie
I know this is now 5 days old but since you are running 8.1, this is what I use to force the end user to right-click when running as a non-admin, if admin it will just continue on.

~DP 8)

Code: Select all

:: ### AdminCheck
 reg query "HKU\S-1-5-19" >NUL 2>&1 && (
 goto admin
 ) || (
        @color 4f
        mode con: cols=80 lines=4
        title, ## Windows 8.1 ADMIN CHECK ##
   ECHO= Right-Click "%~nx0" And Run as Administrator!
   ECHO=
   timeout /t 4 /nobreak>nul
        exit)

:admin
echo You are running this OS As the Built-In Administrator.
echo.
::<Code Here>
pause

Re: How to check for admin rights ?

Posted: 18 Jul 2014 09:13
by Compo
Dos_Probie wrote:I know this is now 5 days old but since you are running 8.1, this is what I use to force the end user to right-click when running as a non-admin, if admin it will just continue on.

~DP 8)

Code: Select all

:: ### AdminCheck
 reg query "HKU\S-1-5-19" >NUL 2>&1 && (
 goto admin
 ) || (
<SNIP>
I remember introducing that method over at MDL

Re: How to check for admin rights ?

Posted: 18 Jul 2014 09:28
by Dragokas
I like one more code from there... with little change:

Code: Select all

set "Priv=OK" & ver |>NUL find "6." && WHOAMI /PRIV |>NUL find /i "SeTakeOwnershipPrivilege" || set "Priv=BAD"