#1 30 Apr 2010 10:23

Teomangia
Member
Registered: 20 Mar 2010
Posts: 26

windows 7: Change Registry,Access Denied

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

#2 30 Apr 2010 10:54

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 762
Website

Re: windows 7: Change Registry,Access Denied

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:
http://ss64.org/viewtopic.php?id=1491

Offline

#3 30 Apr 2010 13:15

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: windows 7: Change Registry,Access Denied

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

#4 03 May 2010 13:07

Teomangia
Member
Registered: 20 Mar 2010
Posts: 26

Re: windows 7: Change Registry,Access Denied

Wow.
Thanks to both.
Optimal solution RG.

Offline

#5 03 May 2010 13:58

Teomangia
Member
Registered: 20 Mar 2010
Posts: 26

Re: windows 7: Change Registry,Access Denied

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

#6 03 May 2010 14:19

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: windows 7: Change Registry,Access Denied

Can you run the calling program as administrator?


CMD and InstallShield

Offline

#7 03 May 2010 14:57

Teomangia
Member
Registered: 20 Mar 2010
Posts: 26

Re: windows 7: Change Registry,Access Denied

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.

Offline

#8 06 May 2010 00:40

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: windows 7: Change Registry,Access Denied

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

#9 06 May 2010 18:38

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 762
Website

Re: windows 7: Change Registry,Access Denied

^ 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

#10 06 May 2010 18:54

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: windows 7: Change Registry,Access Denied

Probably...

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


CMD and InstallShield

Offline

#11 07 May 2010 10:40

Teomangia
Member
Registered: 20 Mar 2010
Posts: 26

Re: windows 7: Change Registry,Access Denied

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

Board footer

Powered by FluxBB