Code: Select all
for /f "tokens=2" %%a in ('netstat -b') do (
if '%%a'=='requested' (
echo Not admin
) else (
echo Admin
))
Any faster / simpler way to know this?
Moderator: DosItHelp
Code: Select all
for /f "tokens=2" %%a in ('netstat -b') do (
if '%%a'=='requested' (
echo Not admin
) else (
echo Admin
))
Code: Select all
@echo off
(at 2>nul)&&goto :YourCode
echo This program must be run with administrative privileges.
echo Please run it right-clicking the file and selecting "Run as administrator".
::or something like that
echo,
pause
exit /b
:YourCode
cls
::your code comes here
Code: Select all
@echo off
md "%windir%\a-test" 2>nul&& (echo You're admin & rd "%windir%\a-test") || echo Not admin
foxidrive wrote:This might work too.Code: Select all
@echo off
md "%windir%\a-test" 2>nul&& (echo You're admin & rd "%windir%\a-test") || echo Not admin
Creating and removing a temporary file/folder is more like a hack. How is this better? This hack depends on the fact that permissions are correct. If a non-elevated, non-admin user has write access to the "%WINDIR%", then the hack would break.Fawers wrote:foxidrive wrote:This might work too.Code: Select all
@echo off
md "%windir%\a-test" 2>nul&& (echo You're admin & rd "%windir%\a-test") || echo Not admin
That's a faster, more secure way, I think.
Code: Select all
at > nul
if %errorlevel% neq 0 (
echo No administrative privileges.
)
XP1 wrote:Checking the error code of "at" is a better hack because you don't have to create temporary files.Code: Select all
at > nul
if %errorlevel% neq 0 (
echo No administrative privileges.
)
You bring up an interesting point that it gets more complicated. I am hoping for better solutions that do not involve this hackery.foxidrive wrote:XP1 wrote:Checking the error code of "at" is a better hack because you don't have to create temporary files.Code: Select all
at > nul
if %errorlevel% neq 0 (
echo No administrative privileges.
)
Of course what you should do if you are really concerned about levels of access is to check exactly who has access to AT.EXE as the backup operator level of access would be able to schedule tasks, not just admin.
Code: Select all
>runas /user:guest "cmd.exe"
Geef het wachtwoord voor guest op:
Poging om cmd.exe als gebruiker ED-SERV-0\guest te starten...
Code: Select all
>echo.%username%
Guest
>net localgroup "administrators"
@echo off
net localgroup administrators 2>nul |find "%username%">nul || (
echo not admin, go away or I shall taunt you a second time!
pause
goto :EOF
)
echo admin continues here
foxidrive wrote:So this is really a definitive way of proving admin permissions of the user.
If it is executed in Win7 by a normal user and 'run as administrator' then it works too.@echo off
net localgroup administrators 2>nul |find "%username%">nul || (
echo not admin, go away or I shall taunt you a second time!
pause
goto :EOF
)
echo admin continues here