ProTek2
asked on
How to elevate permission to batch install app
I need to streamline the installation of an application for computers running various OS (XP, Vista, Win 7) with extremely] novice users. I've been working with the following code (found elsewhere on this site where it was unfortunately not credited to the author) that seems to work well in identifying the right OS. Everything works until the Call %ELEVATED_CMD line.
@echo off
:: ****************** Prompt for elevated permissions *************************
:: This makes the batch file prompt for elevated permissions on Windows Vista
:: or Windows 7, then re-run itself
VER | FINDSTR /IL "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=XP
VER | FINDSTR /IL "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2003
VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=Vista
VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=7
If "%Version%"=="XP" GoTo SkipElevation
If "%Version%"=="2003" GoTo SkipElevation
PushD "%~dp0"
If Exist "%~0.ELEVATED" GoTo SkipElevation
:: Have to escape double quotes because they are passed to Cmd.exe via ShellExecute
Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').Shell Execute('C md.exe', '/C %CMD_Args%', '', 'runas')
echo.
echo Calling this file to re-run with elevated privileges
echo.
Call %ELEVATED_CMD%
echo.
echo Paused to read error message before exit
echo.
pause
Exit
:SkipElevation
If Exist "%~0.ELEVATED" Del "%~0.ELEVATED"
:: ************************** ********** ********** ********** ********** **********
:: program install command here.
It fails on line 1, character 75 but, I don't understand exactly what to expect in the way for a prompt or whether it should just running with advanced permissions. I'm a far cry from being an accomplished script guy but this looks to be almost perfect so I really need help.
It occurs to me that many novice users operate with admin permissions so the script would run so I should also probably only test for users who don't have admin privileges.
@echo off
:: ****************** Prompt for elevated permissions *************************
:: This makes the batch file prompt for elevated permissions on Windows Vista
:: or Windows 7, then re-run itself
VER | FINDSTR /IL "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=XP
VER | FINDSTR /IL "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2003
VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=Vista
VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=7
If "%Version%"=="XP" GoTo SkipElevation
If "%Version%"=="2003" GoTo SkipElevation
PushD "%~dp0"
If Exist "%~0.ELEVATED" GoTo SkipElevation
:: Have to escape double quotes because they are passed to Cmd.exe via ShellExecute
Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').Shell
echo.
echo Calling this file to re-run with elevated privileges
echo.
Call %ELEVATED_CMD%
echo.
echo Paused to read error message before exit
echo.
pause
Exit
:SkipElevation
If Exist "%~0.ELEVATED" Del "%~0.ELEVATED"
:: **************************
:: program install command here.
It fails on line 1, character 75 but, I don't understand exactly what to expect in the way for a prompt or whether it should just running with advanced permissions. I'm a far cry from being an accomplished script guy but this looks to be almost perfect so I really need help.
It occurs to me that many novice users operate with admin permissions so the script would run so I should also probably only test for users who don't have admin privileges.
ASKER
That post is using the exact same code segment that I found and if you lift it out of that post and run it, (Win 7 32 bit) you will find that it returns the exact same error.
What I want to know is why it fails and how to make it work.
What I want to know is why it fails and how to make it work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I solved the problem myself.
Source: http://www.wilderssecurity.com/showthread.php?t=267862
Open in new window