0

The script that I am using is from here: https://stackoverflow.com/a/12264592/1016343

I have a batch file that is made for installing Office 365 using their click-to-run installer. It will first check if the directory that contains the XML configurations and setup.exe exists. Once that is confirmed, it will begin Elevate.cmd - Version 4. The only modifications I have made were removing the following lines:

  ECHO.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  ECHO **************************************
 CLS
 ECHO.
 ECHO =============================
 ECHO Running Admin shell
 ECHO =============================
 REM Run shell as admin (example) - put here code as you like
 ECHO %batchName% Arguments: P1=%1 P2=%2 P3=%3 P4=%4 P5=%5 P6=%6 P7=%7 P8=%8 P9=%9
 cmd /k

My code is supposed to start after these lines:

 ::::::::::::::::::::::::::::
 ::START
 ::::::::::::::::::::::::::::

Whenever I run the batch file, I get a UAC prompt. After I accept the prompt, it closes. To confirm that my code is not executed, I added @echo HELLO! > hello.txt right before call setup.exe %config%, but I could not find the file in the directory. It was not in C:\Windows\System32 either.

Here is my batch file, Install.bat:

@echo off
set directory="<PATH EXCLUDED FOR PRIVACY. ALSO ON A NETWORK DRIVE.>"

if not exist %directory% goto :install_not_found

::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
 @echo off

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~0"
 for %%k in (%0) do set batchName=%%~nk
 set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
 setlocal EnableDelayedExpansion

:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)

  ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  ECHO Next >> "%vbsGetPrivileges%"

  if '%cmdInvoke%'=='1' goto InvokeCmd 

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
 "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /B

:gotPrivileges
 setlocal & cd /d %~dp0
 if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

 ::::::::::::::::::::::::::::
 ::START
 ::::::::::::::::::::::::::::

:: https://www.computerhope.com/forum/index.php?topic=75310.0
:choice
@echo Enter the number for which install you would like:
@echo [1] Office 32-Bit
@echo [c] Cancel
goto :listen

:listen
set /P c=^> 
if /I "%c%" EQU "1" (
set config="/configure office_32.xml"
goto: install
)
if /I "%c%" EQU "C" goto :cancel
if /I "%c%" EQU "c" goto :cancel
@echo Invalid entry
goto :listen

:install_not_found
@echo Error: The installation folder could not be found.
goto :cancel

:cancel
@echo Cancelling Installation...
goto :eof

:install
pushd %directory%
elevate
@echo on
call setup.exe %config%
@echo off
popd
@pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
a135
  • 119
  • 1
  • 6
  • I don't see a `goto :` before `elevate`, or a definition of what `elevate` is anywhere. – Ken White Apr 22 '20 at 17:39
  • @KenWhite Ah... I feel like an idiot now :( Edit: I removed it, but my script still doesn't seem to execute. I guess we are getting a little closer now – a135 Apr 22 '20 at 17:47
  • 1
    So add some `ECHO` commands at various locations to see what is and isn't executing, or just comment out all of the `@echo off` lines so you can see it in order. – Ken White Apr 22 '20 at 17:54
  • @KenWhite That sounds like a good idea, I will try that – a135 Apr 22 '20 at 18:14
  • 1
    @KenWhite Dude you are a life saver. It never crossed my mind once to disable the echo. Apparently, there was something wrong with THEIR script. Nonetheless, do you mind if I answer my question? I will give you credit, of course! – a135 Apr 22 '20 at 18:22
  • I don't mind at all. Let me know when you've done so. :-) – Ken White Apr 22 '20 at 18:25
  • I will add however, that I'm not sure if I understand why you've got all of that in a script. My reason for saying that is that I very much doubt that you could even run the office setup installer as a non administrator. I would assume that simply invoking `setup.exe`, _(with or without the XML config file)_, would request elevatation as necessary. – Compo Apr 22 '20 at 20:59
  • Single quotes are not an alternative for double quotes in *batch-file* and *cmd*. You may get errors as single quotes do not enclose a string to make it a safe literal string. Single quotes do exist in the linked post unfortunately. – michael_heath Apr 23 '20 at 03:51
  • to elevate, just use mshta to execute javascript. there's no need for temp files. `MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"` or powershell `powershell -c Start-Process "%~f0" -Verb runas`. use `>nul 2>&1 net session || NOT ELEVATED && ELEVATED` to check for admin rights – ScriptKidd Apr 25 '20 at 06:13

0 Answers0