Elevate HTA file and all console commands that are run from it

Viewed 111

I have an HTA file with a VBScript embedded in it. From the VBScript, I need to call a command prompt command (powercfg /energy) that requires elevated permissions. The command will be called more than once a minute, so I can't have the user dealing with UAC prompts every time I need to run the command.

When I use the line Shell.Run "cmd /k powercfg /energy", 1 in the script, the command fails because elevated permissions are not given. When I try doing ShellApp.ShellExecute "cmd", "/k powercfg /energy", "", "runas", 1, which runs the command with elevated permissions, it opens up a UAC prompt with "yes" and "no" buttons. (This is running on Windows 10 Education Edition).

Obviously I can't have UAC prompts bothering the user multiple times a minute, so I tried implementing the following subroutine in the VBScript to re-run the HTA file in elevated mode from the beginning (it was running with basic permissions before) if it is not already elevated:

Sub ensureAdmin()
    is_admin = isAdmin() ' function that checks for admin permissions in current window

    If is_admin = False Then
        ShellApp.ShellExecute "mshta", "C:\%userprofile%\desktop\gPowerMeter.hta", "", "runas", 1
        Window.Close
        End If
    End Sub

I know that this code successfully relaunches the HTA file with elevated permissions because it pops a UAC prompt with "yes" and "no" buttons.

The problem is, adding this subroutine doesn't seem to affect the ability of the script to run elevated console commands. I get the same results with the two lines that I tried earlier when the HTA was running with basic permissions.

Does anyone have any suggestions on how to run the HTA and all scripts coming from it in elevated mode? I could implement a workaround, but it would be less elegant. Thank you.

NOTE: I'm not having problems getting the HTA to run elevated. I just want all commands executed from that HTA file through the VBScript ".Run('')" command to be elevated.

1 Answers

Unfortunately, what you are trying to do just isn't possible within the constraints of the technologies you are using.

Elevating the MSHTA process does not affect the elevation of the executed shell process you wish to run from it. The best you can hope for is elevating the individual shell process via the runas command. However, as you have already pointed out, this will trigger a UAC (User Account Control) prompt.

The best advice would be to build the processes using a fully-fledged programming language that allows you more control over the elevation of processes via UAC programmatically over using an HTA.


Useful Links

Related