javascriptfunctionhtarunas

Run as admin inside HTA app for JS function


so I have made the following function for my HTA application that deletes some folders, downloads a new one and creates a shortcut on the desktop. It works fine, the problem occurs when I try to add Run as admin function, to execute following commands as administrator.

This is run as admin code which I took from my batch file:

if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)

I tried to add the following code to my function, at the top:

<script type="text/javascript" language="javascript">

function Run() {
var WShell = new ActiveXObject('WScript.Shell');
WShell.Run('cmd /c if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)', 0, true);


WShell.Run('cmd /c PING localhost -n 5 >NUL', 0, true);
WShell.Run('cmd /c start mshta.exe "\\\\fs\\FIle Share\\SA Support\\ZverTools\\programayendebaWAIT.hta"', 0, true);
WShell.Run('cmd /c PING localhost -n 1 >NUL', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM Nasamartleoba.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM Aqti.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM customDeclaration.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM nasamartleoba.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM aqti.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM customdeclaration.exe /T', 0, true);
WShell.Run('cmd /c PING localhost -n 1 >NUL', 0, true);
WShell.Run('cmd /c taskkill /F /IM Nasamartleoba /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM Aqtebi /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM aqtebi /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM aqtebi.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM Aqtebi.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM Aqti /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclarationUpdate.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration.exe /T', 0, true);
WShell.Run('cmd /c taskkill /F /IM CustomDeclaration.exe /T', 0, true);
WShell.Run('cmd /c PING localhost -n 1 >NUL', 0, true);
WShell.Run('cmd /c RD /S /Q "C:\\saagento\\CustomDeclaration"', 0, true);
WShell.Run('cmd /c RD /S /Q "C:\\saagento\\Custom Declaration"', 0, true);
WShell.Run('cmd /c RD /S /Q "C:\\CustomDeclaration"', 0, true);
WShell.Run('cmd /c RD /S /Q "C:\\Custom Declaration"', 0, true);
WShell.Run('cmd /c PING localhost -n 3 >NUL', 0, true);
WShell.Run('cmd /c xcopy "\\\\fs\\FIle Share\\SA Support\\saagento prog\\CustomDeclaration" "C:\\saagento\\CustomDeclaration" /E /I /S /Y /D', 0, true);
WShell.Run('cmd /c xcopy "\\\\fs\\FIle Share\\SA Support\\ZverTools\\CustomDeclarationUpdate.exe - V.lnk" "%USERPROFILE%\\Desktop" /y', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclarationUpdate.exe - Shortcut.lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclarationUpdate.exe - Shortcut (2).lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclarationUpdate - Shortcut.lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclarationUpdate - Shortcut (2).lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclaration.exe - Shortcut.lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclaration.exe - Shortcut (2).lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclaration - Shortcut.lnk"', 0, true);
WShell.Run('cmd /c del /f "%USERPROFILE%\\Desktop\\CustomDeclaration - Shortcut (2).lnk"', 0, true);
WShell.Run('cmd /c start "" "\\\\fs\\FIle Share\\SA Support\\ZverTools\\killlaProgramayendebawait.vbs"', 0, true);
WShell.Run('cmd /c rundll32 user32.dll,MessageBeep', 0, true);
}

</script>

But for some reason it won't work, it works if you will execute it via bat file, but if you try to execute it from HTA application it gives following error: Expected')'.

I think you should use some special characters around ()?


Solution

  • You just don't take into account that the batch doesn't strip the incoming quotes.

    From the batch file, taking into account the need to remove quotes %0, and the call will be

        set p=%0                                                                                    
        set p=!p:"=!
        SET code=""""start -verb runas '%p%'""""
        mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -noexit -command %code%"",0,false:close")
    

    If you need only trust level then

        :: 0x40000  Admin 0x20000 - User
        runas /trustlevel:0x20000 "powershell.exe -noexit -ExecutionPolicy Bypas -command Some-Command-Line"