'File not found when ExecuteGlobal after a ReadAll in VBScript

this might be trivial but I'm writing a VBScript template and I wanted to add the possibility to inject VBScript code from the internet inside VBScript and run it.

The code originally was:

Sub WebImport(URL)
    ' Import the VBS code at a given URL.
    ' and run it globally pushing the functions into the Main
    Dim Request    
    Set Request = createobject ("MSXML2.ServerXMLHTTP")
    Request.Open "GET", URL, false
    Request.Send
    ExecuteGlobal Request.ResponseText
    Set Request = Nothing
End Sub

I already wrote a function to import code, just like Python, and the principle is the same. It works flawlessly.

If the WebImport works as it should, I'll create a basic import that handles them in the same way.

I import the raw code from my own repository here.

It does everything correctly except executing the file globally in the Main scope, I tried with/without deleting the file, the result doesn't change at all, unfortunately...

Edit

This is the full script, it imports WebImport from scr/Functions and runs the code to inject the code from a given repo until it has to write a file, then 800a0046 vbscript at row 0, line 1 kicks in... I'm admin on the machine.

Option Explicit

'                                 VBScript Main File Model
' Author:                            
'      Fabio Craig Wimmer Florey ([email protected])
'
' Reviewed By:                                                  Last Review:
'      Fabio Craig Wimmer Florey ([email protected])     2022-03-29
'
' Description:
'      Main Subroutine


Import "src/Functions"
WebImport "https://raw.githubusercontent.com/nmoosaJHB/Docker-compose-SuiteCRM/714bada98abbb2ab497258c8ea9a726f234aaba3/public_html/vendor/gymadarasz/ace/demo/kitchen-sink/docs/vbscript.vbs"

Sub Main()
  ' Main Subroutine
  MakeHelloWorldFile "Hello.txt"
End Sub

Sub Import(Filename)
  '    Import Code from VBS File, DO NOT DELETE
    Dim Lib, Code, FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Lib = FSO.OpenTextFile(Filename & ".vbs")
    Code = Lib.ReadAll
    Lib.Close
    ExecuteGlobal Code
    Set Lib = Nothing
    Set FSO = Nothing
End Sub

Call Main


Solution 1:[1]

The problem here is not the XHR call that works without needing to try outputting the response to a file. The issue is the FileSystemObject does not support relative paths you need to provide the relative path using code like;

Dim path: path = FSO.GetParentFolderName(WScript.ScriptFullName) & "\"

With the following changes to the Import() procedure the code will run WebImports() from the src\Functions.vbs file.

'Use the correct directory separator
Import "src\Functions"

'... other code excluded for clarity

Sub Import(Filename)
  '    Import Code from VBS File, DO NOT DELETE
    Dim Lib, Code, FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Get path based off the script
    Dim path: path = FSO.GetParentFolderName(WScript.ScriptFullName) & "\"
    'Append path to the filename
    Set Lib = FSO.OpenTextFile(path & Filename & ".vbs")
    Code = Lib.ReadAll
    Lib.Close
    ExecuteGlobal Code
    Set Lib = Nothing
    Set FSO = Nothing
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1