check if user is admin

Hewlett-packard / Sd2100
February 7, 2009 at 12:21:34
Specs: Windows XP, 1024
whats a code that check if use is admin or not and if not goto a messages saying not admin and exit program. ?

See More: check if user is admin

Report •


#1
February 7, 2009 at 14:31:27
I'm sure there is a better way, but here is something that *should* work.

for /f "delims=" %%a in ('2^>^&1 ^>nul defrag -a %systemdrive%') do set notadmin=%%a
if defined notadmin exit /b

Not fully tested......


Report •

#2
February 7, 2009 at 16:05:27
how could i go about puting it at the beginning of my program? it would be different id think .

Report •

#3
February 7, 2009 at 16:46:11
Just insert:

for /f "delims=" %%a in ('2^>^&1 ^>nul defrag -a %systemdrive%') do set notadmin=%%a
if defined notadmin (
echo Not admin!
exit /b
)

At the point that you want to check if it is running as admin, it will exit the script if it's not running as admin.....


Report •

Related Solutions

#4
February 7, 2009 at 16:56:59
kool thanks that what i needed. how well would this work on vista?

Report •

#5
February 7, 2009 at 16:59:48
I'm not sure, but it should be simple for you to test.

Report •

#6
February 9, 2009 at 15:14:04
for /f "delims=" %%a in ('2^>^&1 ^>nul defrag -a %systemdrive%') do set notadmin=%%a
if defined notadmin (
echo Not admin!
exit /b
)

this doesnt work on vista any ideas? it work with xp but not vista anything work with both?

Thanks


Report •

#7
February 9, 2009 at 17:34:11
If you want the most reliable method, then you need to execute the CheckTokenMembership() C function in advapi32.dl which can't be done from a batch file.

If you want, I can give you a program that I wrote in RealBasic that does this and I can modify its output to suite you needs.


Report •

#8
February 9, 2009 at 21:36:21
well i kind need something for batch file. but thanks. anyone out there know of any thing i could use with xp and vista?


Thanks
Brock


Report •

#9
February 9, 2009 at 23:09:08
So, you're saying that you can't execute another program in your batch file and capture its output or exit code?

That is essentially what the for loop is doing, but in that case you're executing the defrag utility.


Report •

#10
February 10, 2009 at 09:31:48
well im using the command to check admin rights and if not admin close the program. works great on xp but on vista it just stalls and doesnt do a thing. is there any other way of checking admin rights under batch?


Brock


Report •

#11
February 10, 2009 at 11:43:29
If you're asking if there is a standard batch command that checks for admin rights, then the answer is NO.

However, the batch file can execute a utility program that returns the admin rights status.

Here are a couple links to VB6 and C++ examples.

http://social.msdn.microsoft.com/fo...

http://blogs.msdn.com/cjacks/archiv...

http://msdn.microsoft.com/en-us/lib...

The RealBasic version that I made (which is gui based that I'd need to convert to a console app for you) was based off of those examples. The main issue I have with RealBasic is that due to its cross platform compatibility, it creates a larger executable than the other languages.


Report •


Ask Question