Option Explicit
Dim shell, fso, msiPath, msiUrl, logPath, logFile

Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

' Log file location
logPath = shell.ExpandEnvironmentStrings("%TEMP%") & "\install_log.txt"
Set logFile = fso.OpenTextFile(logPath, 8, True)

' Logging function
Sub LogMessage(message)
    logFile.WriteLine Now & " - " & message
End Sub

msiUrl = "https://app.action1.com/agent/3e18db44-0dc9-11f1-928c-99532681d6d5/Windows/agent(My_Organization).msi"
msiPath = shell.ExpandEnvironmentStrings("%TEMP%") & "\installer.msi"

LogMessage "Starting download..."

' Download using PowerShell (most reliable)
shell.Run "powershell -Command ""Invoke-WebRequest -Uri '" & msiUrl & "' -OutFile '" & msiPath & "' -UseBasicParsing""", 0, True

WScript.Sleep 3000

If fso.FileExists(msiPath) Then
    LogMessage "Download successful. Starting installation..."
    shell.Run "msiexec /i """ & msiPath & """ /qb /norestart", 1, True
    fso.DeleteFile msiPath, True
    LogMessage "Installation completed."
Else
    LogMessage "Failed to download installer."
End If

logFile.Close
WScript.Quit 0
