Search code examples
jscripthtawshelevated-privileges

Make an HTA file run as admin (elevated)


In wsf, vbs and js files you can easily find out if they run elevated and if not you can easily make them.
The code I've written for that is this:

EnsureElevatedPrivileges();
WScript.Echo("Running elevated now!");

function EnsureElevatedPrivileges() {
    if (!WScript.Arguments.Named.Exists("elevate")) {
        new ActiveXObject("Shell.Application").ShellExecute(WScript.FullName, "\"" + WScript.ScriptFullName + "\" /elevate", "", "runas", 1);
        WScript.Quit();
    }
}

But this doesn't work in HTA files because the global WScript variable doesn't exist there.
So how can I force a specific HTA file run (itself) as admin?


Solution

  • You can take a look at this : Procedure to run HTA elevated

    <html> 
    <head> 
    <title>HTA Helpomatic</title> 
    
    <HTA:APPLICATION 
         ID="oHTA" 
         APPLICATIONNAME="HTAHelpomatic" 
         SCROLL="yes" 
         SINGLEINSTANCE="yes" 
    > 
    <!-- ID="objHTAHelpomatic" --> 
    <!-- WINDOWSTATE="maximize" --> 
    
    </head> 
    
    <SCRIPT Language="VBScript"> 
    
    If HTAElevate() = True Then 
        CreateObject("WScript.Shell").Run "mmc.exe compmgmt.msc", , True 
        Call Main() 
    End If 
    
    Sub Main() 
        MsgBox "HTA-Ende", 4096 
    End Sub 
    
    
    '*** v13.3 *** www.dieseyer.de ***************************** 
    Function HTAElevate() 
    '*********************************************************** 
    ' Unter Windows x64 laufen VBS' nach einem Doppelklick in der x64-Umgebung 
    ' mit %WinDi%\System32\wscript.exe oder mit %WinDi%\System32\cscript.exe. 
    ' In der x64-Umgebung laufen VBS aber nicht (richtig). Die Prozedur 
    ' HTAElevate() erkennt dies und startet ggf. das VBS in der 
    
      Const Elev = " /elevated" 
    
    ' MsgBox oHTA.commandLine, , "5016 :: " 
    
    ' Trace32Log "5018 :: oHTA.commandLine: ==" & oHTA.commandLine & "==", 1 
    
      HTAElevate = True 
    
    ' If InStr( LCase( oHTA.commandLine ), Elev) > 0 then MsgBox oHTA.commandLine, , "5022 :: " 
      If InStr( LCase( oHTA.commandLine ), Elev) > 0 then Exit Function 
    
    
      On Error Resume Next 
        window.resizeto 750, 10 ' : window.moveto screen.width / 2, screen.height / 2 
      On Error GoTo 0 
    
    ' MsgBox oHTA.commandLine, , "5030 :: " 
    
      createobject("Shell.Application").ShellExecute "mshta.exe", oHTA.commandLine & Elev, "", "runas", 1 
    
      HTAElevate = False 
    
      self.close 
    
    End Function ' HTAElevate() 
    
    
    </SCRIPT> 
    <body> 
    
    
    </body> 
    </html>