0

I created a batch to run specific commands, the code looks like this:

cd  D:\projects\Project Stress Test\signed one\com0com\x64
setupc
pause

what i want is to run the setupc file as an admin?

i tried runas /user:<Name>\administrator commands but it didnt work.

is there any easy way to do that?

13
  • 1
    Don't you need quotes around that path? It has spaces in it.
    – Blorgbeard
    Commented Dec 7, 2012 at 2:50
  • Make sure you're using runas correctly. Commented Dec 7, 2012 at 2:51
  • 4
    Well what does "it didn't work" mean? runas /user:YOURHOSTNAME\Administrator setupc should work,
    – Blorgbeard
    Commented Dec 7, 2012 at 2:56
  • 1
    @Blorgbeard: for cd command, it will work. it probably assimilates all the argv's to form a single path. I have, however, not tested it against 2 consecutive spaces, like "project<space><space>stress<space>test"
    – anishsane
    Commented Dec 7, 2012 at 6:38
  • 1
    You have to enter password manually to 'runas' command. If you are trying to do it in automated way, it will not work as is. For that, try scheduled task or such option.
    – anishsane
    Commented Dec 7, 2012 at 6:39

1 Answer 1

3

You could make the entire script run at admin level. Here is a batch function I use in my scripts.

@echo off
call :Admin xReturn true 1
echo.%xReturn%
goto End

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if needed.
:: NOTE: This will restart the script with Admin privs if Needed is set to 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
if not "%~3"=="" set xSuccess=%~3

:: Check for Access
::net session >nul 2>&1
>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

:End
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.