Pass a comand on a batch with admin privileges

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Pass a comand on a batch with admin privileges

#1 Post by hacxx » 26 Dec 2020 12:28

I have updated 2 of my tools with a admin privileges script.

IFEO Tool V2
https://filecrypt.cc/Container/509C3613D0.html

Firewall Blocker for Windows V3
https://filecrypt.cc/Container/DADEB77E7D.html

The problem is if i pass a command (for example: bat.bat /test1) /test1 will not pass along.
Is there any fix for this?

The code i added is here:

Code: Select all

@echo off
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    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 "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------  
Thanks

Check my "batches"
http://www.cyberlord.at/forum/?id=10589&forum=73587

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Pass a comand on a batch with admin privileges

#2 Post by Eureka! » 27 Dec 2020 05:24

Start DEMO.cmd with some parameters, like DEMO a b c d


DEMO.cmd

Code: Select all

@echo off
setlocal

::  Check if elevated; restart as elevated admin if not. 
    whoami.exe /groups|findstr /i /c:"S-1-5-32-544" > nul || goto :NEEDADMIN


    REM Your code here
    echo This is elevated code
    echo Demo : current (elevated) commandline =   "__%*__"
    pause


goto :EOF



::____________________________________________________________
::
                :NEEDADMIN
::____________________________________________________________
::

::  Create dummy script
    set RANDOM=>"%temp%\deleteme.AUTOELEVATE"
	
::  File association: Run .AUTOELEVATE always elevated
    reg add "HKCU\Software\Classes\.autoelevate\Shell\runas\command" /ve /D "cmd.exe /C \"\"%~f0\"   %%*\" " /F >nul


::  Execute dummy .ELEVATE script
    start "" "%temp%\deleteme.AUTOELEVATE" %*

::  Clean up and exit
    reg delete HKCU\Software\Classes\.autoelevate /F >nul
    del /Q "%temp%\deleteme.autoELEVATE"

goto :EOF

Last edited by Eureka! on 28 Dec 2020 08:27, edited 2 times in total.

hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Re: Pass a comand on a batch with admin privileges

#3 Post by hacxx » 27 Dec 2020 09:44

Eureka! wrote:
27 Dec 2020 05:24
Start DEMO.cmd with some parameters, like DEMO a b c d
Do you have any code that doesn't require adding registry keys?
I just want to ask for elevation and pass a parameter, this way it can be autostart from a rar sfx...

Check my "batches"
http://www.cyberlord.at/forum/?id=10589&forum=73587

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Pass a comand on a batch with admin privileges

#4 Post by Eureka! » 28 Dec 2020 08:40

hacxx wrote:
27 Dec 2020 09:44
Do you have any code that doesn't require adding registry keys?
What is wrong with adding registry keys? (they are removed too, btw)
I just want to ask for elevation and pass a parameter, this way it can be autostart from a rar sfx...
That is what this script does!
I edited the original code a little to make it more obvious (I thought it was, but apparently not)


For you vbs solution: you will probably have to use Chr(34) instead of double quotes.
Something like this (entirely untested as I don't use vbs anymore) :

Code: Select all

UAC.ShellExecute "cmd.exe",  "/c  " & Chr(34) & "%~f0" & Chr(34) & " %*", "", "runas", 1

BTW: Instead of your "%PROCESSOR_ARCHITECTURE%" routine, you can also use wmic.exe OS get OSArchitecture.
(you can run a 32-bit Windows on a 64-bit processor).

hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Re: Pass a comand on a batch with admin privileges

#5 Post by hacxx » 30 Dec 2020 12:50


Post Reply