Quantcast
Channel: HTA – Alex Computer Bubble Blog
Viewing all articles
Browse latest Browse all 4

Run a Program Elevated via the Right Click Menu – Run as Administrator

$
0
0

By definition, an HTML Application (HTA or HTAs) is a Microsoft Windows application which source code consists of HTML and one of scripting languages supported by Internet Explorer (VBScript or Jscript). The file extension of an HTA is .hta, and it is executed using the program mshta.exe. I personally like HTA because its graphical user interface enables me to overcome some of the limitations of the command window, and makes it easier to run my scripts. Unfortunately HTA does not automatically trigger a User Account Control, it doesn’t have the “Run as administrator” in the context menu, and there is no compatibility tab where it could be configured to run as administrator.
To adjust the context menu (for an HTA file) so that you can launch the application as administrator, there is a simple registry tweak that has been tested on Windows XP, Vista, Windows 7 and Windows 8.
The following registry script (CreateContextMenuForHTA.reg) creates this context menu for HTA files:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\htafile]
@=”HTML Application”
[HKEY_CLASSES_ROOT\htafile\DefaultIcon]
@=”C:\\WINDOWS\\system32\\mshta.exe,1″
[HKEY_CLASSES_ROOT\htafile\Shell]
[HKEY_CLASSES_ROOT\htafile\Shell\RunAs]
[HKEY_CLASSES_ROOT\htafile\Shell\RunAs\Command]
@=”C:\\WINDOWS\\system32\\mshta.exe \”%1\” %*”

In the case that a registry tweak would not be allowed as a solution, that you can run an HTA in elevated mode by using the following VBScript (RunHtaAsAdministrator.vbs) which assumes that you have a folder C:\MyHtaScripts where you keep your HTA files.

' Filename:    RunHtaAsAdmin.vbs
' Date:        Jan. 2013
' Author:      Alex Dujakovic
' Description: Run HTA file as administrator
' ************************************************************
strHTAfolder = "C:\MyHtaScripts"

RunAsAdminSelectedHTA()
Sub RunAsAdminSelectedHTA
  Set objShell = CreateObject("Shell.Application")
  Set objFolder = objShell.Namespace(strHTAfolder)
  Set objFolderItem = objFolder.ParseName(ChecUserInput())
  objFolderItem.InvokeVerb "runas"
End Sub
Function ChecUserInput
  strHtaAppName = InputBox("Enter the name of your HTA Application", "HTA Application - Run As Administrator")
  If(strHtaAppName) <> "" Then
    Set fso = CreateObject("Scripting.FileSystemObject")
      If fso.fileExists(strHTAfolder & "\" & strHtaAppName) Then
        ChecUserInput = strHtaAppName
      Else
        MsgBox "Typed name is incorrect, please try again."
        ChecUserInput()
      End If

  ElseIf(strHtaAppName) = vbNullString Then
    MsgBox "That is an incorrect entry, please try again"
    WScript.Quit
  End If
End Function




Please note: Although the author has made every reasonable attempt to achieve complete accuracy of the content, he assumes no responsibility for errors or omissions. Also, you should use this information as you see fit, and at your own risk.


Viewing all articles
Browse latest Browse all 4

Trending Articles