param( [string]$configFile = "$PSScriptRoot\MSIProperties.json" ) $ErrorActionPreference = "Stop" $logPath = "$env:windir\Temp\InstallStatus.log" Start-Sleep -Seconds 60 try { if (-not (Test-Path -LiteralPath $configFile)) { Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Config file not found: $configFile" exit 1 } $config = Get-Content -LiteralPath $configFile -Raw | ConvertFrom-Json $expectedVersion = [string]$config.expectedVersion $regKey = "HKLM:\SOFTWARE\ManageEngine\FAP" if ([Environment]::Is64BitOperatingSystem) { $productCode = "{859C3CA2-0CD2-4A38-8993-07D53F581E40}" $msiFile = "$PSScriptRoot\DataSecurityPlusAgent-x64.msi" } else { $productCode = "" $msiFile = "$PSScriptRoot\DataSecurityPlusAgent-x86.msi" } if (-not (Test-Path -LiteralPath $msiFile)) { Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] MSI not found: $msiFile" exit 2 } if (Test-Path $regKey) { try { $installedVersion = [string](Get-ItemProperty $regKey).Build if ($installedVersion -ge $expectedVersion) { Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Installed version ($installedVersion) >= expected ($expectedVersion). Skipping." exit 0 } Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Uninstalling version $installedVersion" if ($productCode) { $u = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $productCode /qn /norestart /l*v C:\Windows\Temp\DSP_Uninstall.log" -Wait -PassThru Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Uninstall exit code: $($u.ExitCode)" } } catch { Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Failed to read uninstall info: $($_.Exception.Message)" } } $msiArgs = @( "/i `"$msiFile`"", "PROTOCOL=$($config.PROTOCOL)", "PORT=$($config.PORTNO)", "SERVERNAME=$($config.SERVERNAME)", "FQDN=$($config.FQDN)", "SERVERIP=$($config.SERVERIP)", "ISENDPOINTAUTOINSTALLREQUIRED=$($config.ENDPOINTAUTOINSTALLREQUIRED)", "AGENTINSTALLATIONKEY=$($config.AGENTINSTALLATIONKEY)", "/qn /norestart /l*v C:\Windows\Temp\DSP_Install.log" ) -join " " $p = Start-Process -FilePath "msiexec.exe" -ArgumentList $msiArgs -Wait -PassThru Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Install exit code: $($p.ExitCode)" exit $p.ExitCode } catch { Add-Content -Path $logPath -Value "[$(Get-Date -Format u)] Fatal error: $($_.Exception.Message)" exit 99 }