?
Solved

How to elevate permission to batch install app

Posted on 2011-02-17
4
Medium Priority
?
1,777 Views
Last Modified: 2012-05-11
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').ShellExecute('Cmd.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.
0
Comment
Question by:ProTek2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 47

Expert Comment

by:Jackie Man
ID: 34922722
The information below maybe helpful for you.

Source: http://www.wilderssecurity.com/showthread.php?t=267862
Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('Cmd.exe', '/C %CMD_Args%', '', 'runas')
Echo %ELEVATED_CMD% >> "%~0.ELEVATED"
:: If there are single quotes in the arguments, this will fail
Call %ELEVATED_CMD%
Exit
:SkipElevation
If Exist "%~0.ELEVATED" Del "%~0.ELEVATED"

Open in new window

0
 

Author Comment

by:ProTek2
ID: 34923050
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.
0
 

Accepted Solution

by:
ProTek2 earned 0 total points
ID: 34926228
I discovered the problem. One of the folder names in the path contained an apostrophe. Eliminating that eliminated the error.
0
 

Author Closing Comment

by:ProTek2
ID: 34959110
I solved the problem myself.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question