For the past week or so I'm trying out a new way to "deploy" a folder to another computer's desktop, download a couple of files in it and then run one of them as admin. The only thing is the file that does all this needs to be really lightweight, like under 500KB or so, or else it's useless.
What I'm doing so far is: I'm using a WinRar-created SFX archive called Program.exe that contains 2 files, the run.bat file and the winhttpjs.bat that I use to download the other files (https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/winhttpjs.bat)
The run.bat contains this code:
@echo off
MKDIR "%userprofile%\Desktop\Folder1"
CALL winhttpjs.bat "https://docs.google.com/uc?export=download&id=id1" -saveTo "%userprofile%\Desktop\Folder1\File1.bat"
CALL winhttpjs.bat "https://docs.google.com/uc?export=download&id=id2" -saveTo "%userprofile%\Desktop\Folder1\winhttpjs.bat"
START "%userprofile%\Desktop\Folder1\File1.bat"
EXIT
And the File1.bat file contains the following code:
@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 )
exit
: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
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
echo This is the start of the script
echo.
GOTO START
:START
IF EXIST "%userprofile%\Desktop\Program.exe" (DEL "%userprofile%\Desktop\Program.exe")
CD /D "%~dp0"
rest of code...
The problem with this approach is the fact that, once it starts the downloaded File1.bat, that file asks for admin rights and once I click "yes" it just opens up another command prompt which asks for admin rights and that happens over and over again, like in a continuous loop. If I do not ask for admin rights the file works just as intended. So I'm guessing it's either a problem with how I'm asking for admin permissions or how it acts regarding the fact that it's a SFX archive or it might be something else entirely.
Do you guys have any idea how to make this work and as space-efficient as possible? I'm open to any idea whatsoever like changing how I download files, how I ask for admin rights, how not to use a SFX archive but something else etc
Note: I'm only using winhttpjs because of the really small file size (20KB) compared to popular alternatives like wget or curl and I'm only using it to download a couple of .bat files from Google Drive, one of it being itself (I'm using it to also download more files later in the script, but that's irrelevant). If I could somehow download the files directly from the run.bat file without needing to call on winhttpjs, or if I could modify the winhttpjs to also download the 3 files so I could skip the run.bat altogether that would be great (unfortunately BITS is slow and deprecated).
Invoke-WebRequest
could be sufficient?