Search code examples
vbscriptpermissionsadminhta

File not found when executing script as admin


I have this script that launches an HTA which needs to be started with admin rights.

Set objShell = CreateObject("Wscript.Shell")

isLocal = MsgBox("Launch app for a local configuration ?", vbYesNo + vbQuestion, "Settings")
If isLocal = vbYes Then
    objShell.Run "src\Configurator.hta"
Else
    'This code doesn't matter here
End If

This script runs fine when started normally, but when I execute the VBS as Administrator (via context menu), I get a File Not Found error for the objShell.Run "src\Configurator.hta" line.

When I add the following code, it gives the same result for both executionning methods (gives the directory where the script is executed).

scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
MsgBox scriptdir

Any help or explanation on this issue would be greatly appreciated.


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>