== Permissions Issue Fix with "Access Denied" ==

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

== Permissions Issue Fix with "Access Denied" ==

#1 Post by Dos_Probie » 22 Dec 2012 07:01

If you are getting a "Access Denied" or "Invalid Path" from the commandline window when you attempt to copy or make
any changes to the Program files, Windows directory or any subdirectory from there then you are not alone..
Seems these are "Protected Folders" requiring "Elevated Administrator Privileges" when using NTFS. You could always do
the obligatory right-click and "Run as administrator" or just work off a non-protected directory ie: Systemdrive,
User Appdata, Programdata etc. but sometimes that is not a option. Anyway I had a issue from a earlier
post of mine on here where I was attempting to copy files over and then call a batch file to and from the system32 folder until
I thankfully found this fix over at the Stackoverflow forum allowing to runas administrator from a batch file..
Hope this helps anyone else having this issue in the future..Happy Holidays. :mrgreen:

Code: Select all

@echo off
:: Credits: http://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file
::          https://sites.google.com/site/eneerge/scripts/batchgotadmin

:: BatchGotAdmin
:: Bypass UAC and Run your batch as admin..
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

<YOUR BATCH SCRIPT HERE>

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: == Permissions Issue Fix with "Access Denied" ==

#2 Post by DigitalSnow » 26 Dec 2012 16:44

Admin UAC elevation is an important step now days in Batch scripts. Just wanted to contribute and add the Admin batch routine that I use.

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"
 
:: Check for Access
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set "xAdmin=%xSuccess%"
 
:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
  echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
  echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
   if exist "%xVBUAC%" (
      "%xVBUAC%"
      rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
      del "%xVBUAC%"
   )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof
:: by David Ruhmann
:: https://gist.github.com/4280586

Post Reply