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.