0

I want to run batch file with admin. So I put these code at the beginning of the batch file.

@echo on
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
if '%errorlevel%' NEQ '0' (
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    del /f /q "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
Pushd "%~dp0"

But it didn't work.

I use "pause" to examine where is bug.

I found that when executing this line "%temp%\getadmin.vbs"

UAC will prompt.

If I directly execute C:\Users\User\AppData\Local\Temp\getadmin.vbs in CMD.

UAC will prompt too.

But another stranger thing is if I run the code above on another computer,

it is totally fine. No UAC prompt.

I try the code from here also useless.

How to solve it?


Update

::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
 @echo on
 CLS
 ECHO.
 ECHO =============================
 ECHO Running Admin shell
 ECHO =============================

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~dpnx0"
 rem this works also from cmd shell, other than %~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.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  ECHO **************************************

  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
 ::::::::::::::::::::::::::::
 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

I try the code from here

Just change echo off to echo on. No add any code.

Setting UAC to this. UAC Setting

execute the BAT file directly. and it will stop at "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*

Like this

And then I open 0EgetPriv_ST.vbs

add ' at UAC.ShellExecute "C:\Windows\System32\cmd.exe", args, "", "runas", 1

like this

Set UAC = CreateObject("Shell.Application") 
args = "ELEV " 
For Each strArg in WScript.Arguments 
args = args & strArg & " "  
Next 
args = "/c """ + "C:\Users\User\Desktop\ST.bat" + """ " + args 
'UAC.ShellExecute "C:\Windows\System32\cmd.exe", args, "", "runas", 1 

It can execute normally.

No UAC prompt.

Why UAC.ShellExecute "C:\Windows\System32\cmd.exe", args, "", "runas", 1 cause UAC prompt ?

And how to avoid it?

fourgoats
  • 1
  • 1
  • 1
    Please do not delete [your closed question](https://stackoverflow.com/q/73329557), then repost it. The idea is that you edit the closed question to address the reasons for closure. Any modifications will be assessed, and votes will be cast, to determine whether sufficient improvement has been made for reopening it. – Compo Aug 13 '22 at 10:01
  • 1
    It appears to me, after all of that, your issue seems to be that you are wanting to pass one or more arguments to the vbscript, which I assume, is breaking the command. How about you begin by giving us a realistic, for your actual project, example of the cmd.exe command line you think you want. Then we can try to work backwards to determine what must be passed to vbscript initially to generate that command line correctly. – Compo Aug 13 '22 at 10:12
  • @Compo my actual project just run `diskpart /s C:\Users\User\Desktop\RemoveLetter.txt`, and the code in txt file is `list vol sel vol 2 remove letter=E`. As you can see, I want to do is remove HDD's letter, but it need execute with admin, I try a lot of code, but all didn't work fine. – fourgoats Aug 14 '22 at 02:58
  • I try to find out why @Matt's and other people's code didn't work. Finally, I noticed that executing the VBS file directly will cause problems. As article say, vbs file running `UAC.ShellExecute "C:\Windows\System32\cmd.exe", args, "", "runas", 1 ` will UAC prompt. But it shouldn't do this normally. – fourgoats Aug 14 '22 at 03:08
  • Modify the text `I try a lot of code, but all didn't work fine.` I try a lot of code to avoid UAC prompt, but all didn't work fine . by the way: Diskpart Bat file work fine. – fourgoats Aug 14 '22 at 03:12
  • What do you mean avoid UAC prompt? The idea of all of the code you're using is to elevate permissions, and to do that, unless the PC has been configured otherwise, the UAC prompt will still appear, and require acceptance. – Compo Aug 14 '22 at 08:57

0 Answers0