Change Registry, Access Denied

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Change Registry, Access Denied

Post by MigrationUser »

30 Apr 2010 10:23
Teomangia

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

----------------------------

#2 30 Apr 2010 10:54
Simon Sheppard


Create a shortcut to the batch file, in the shortcut properties you can make it run with elevated permissions.

Edit: also see this later thread with a lot more detail:
viewtopic.php?f=2&t=40

----------------------------

#3 30 Apr 2010 13:15
RG


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.

Code: Select all

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.

Code: Select all

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)

Windows Shell Scripting and InstallShield

----------------------------

#4 03 May 2010 13:07
Teomangia


Wow.
Thanks to both.
Optimal solution RG.

----------------------------

#5 03 May 2010 13:58
Teomangia


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.

----------------------------

#6 03 May 2010 14:19
RG


Can you run the calling program as administrator?

Windows Shell Scripting and InstallShield

----------------------------

#7 03 May 2010 14:57
Teomangia
RG wrote:

Can you run the calling program as administrator?
The batch that I make must be made by an administrator. wink
He wanted to be a curiosity.
Thanks a lot.

----------------------------

#8 06 May 2010 00:40
RG

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:

Code: Select all

    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)

Windows Shell Scripting and InstallShield

----------------------------

#9 06 May 2010 18:38
Simon Sheppard


^ thats interesting, I wonder if a rogue script could enumerate the registry and just overwrite the first RunAsAdmin script it finds with something different.

----------------------------

#10 06 May 2010 18:54
RG


Probably...

Last edited by RG (07 May 2010 16:53)

Windows Shell Scripting and InstallShield

----------------------------

#11 07 May 2010 10:40
Teomangia


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.
Post Reply