You are not logged in.
Hello everyone.
I have to run a batch to change a registry key.
REG ADD....
It returns the message "access denied".
If i run the Command Prompt in the "elevated mode" it's OK, but i need to run with a batch.
Thanks
Offline
Create a shortcut to the batch file, in the shortcut properties you can make it run with elevated permissions.
Offline
Or 'right click' on bat file and 'Run as administrator'.
I add the following code to bat files to check for admin privileges (if needed) at the start so that I can throw an error and exit if non-admin.
Behavior varies by OS, as you know. I use both snippets of code below. For your described problem you may only need the second one.
This code takes advantage of the fact that admin privileges are required for the OPENFILES command.
I pipe the output to null, as we don't care about the output.
Check errorlevel. If the command failed we are not admin; if it worked we are admin.
VER | FIND "Version 5.2." > nul
IF %ERRORLEVEL% == 0 (
REM Newer than 2003 Server
REM Do OPENFILES to check for administrative privileges
OPENFILES > nul
IF ERRORLEVEL 1 (
COLOR CF
ECHO.You must be logged on as Administrator to run this program'.
EXIT /B 1
)
)For newer OS's you need this because even if you are logged on as administrator the bat file is started with reduced privileges.
VER | FIND "Version 6." > nul
IF %ERRORLEVEL% == 0 (
REM Newer than Vista
REM Do OPENFILES to check for administrative privileges
OPENFILES > nul
IF ERRORLEVEL 1 (
COLOR CF
ECHO.Right click on this bat file and select 'Run as administrator'.
EXIT /B 1
)
)Last edited by RG (30 Apr 2010 13:33)
CMD and InstallShield
Offline
Wow.
Thanks to both.
Optimal solution RG.
Offline
OK.
But, in this moment, there's another problem.
If i want to call this file from another file ?
Is impossible to use the right click.
Thanks.
Offline
Can you run the calling program as administrator?
CMD and InstallShield
Offline
Can you run the calling program as administrator?
The batch that I make must be made by an administrator. ![]()
He wanted to be a curiosity.
Thanks a lot.
Offline
There is another way to handle this, but please look before you leap! This solution is less than ideal if you are doing this on multiple machines. However, If you are doing this on the same machine that is under your control you can modify the registry so that your bat file automatically runs as administrator. To do so you just need to add a new String Value to the registry at:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
Example: C:\Users\Me\Desktop\MyBatFile.Bat RUNASADMIN
Where the String Value Name is the FullPathAndFileName of your bat file and the value is RUNASADMIN as shown in the above example.
Please consider the security risks based on your situation.
Last edited by RG (06 May 2010 00:41)
CMD and InstallShield
Offline
^ thats interesting, I wonder if a rogue script could enumerate the registry and just overwrite the first RunAsAdmin script it finds with something different.
Offline
Probably...
Last edited by RG (07 May 2010 16:53)
CMD and InstallShield
Offline
Hello. As I said my batch must be made by an administrator. Initially I had this problem but I solved by restarting my machine (I do not know how). However the solutions I have suggested are very interesting. Thanks.
Offline