$ErrorActionPreference = "Stop" $AlloyUrl = "https://updater.3dnetmedical.com/alloy-installer-windows-amd64.exe" $ConfigUrl = "https://updater.3dnetmedical.com/alloy.txt" $Installer = Join-Path $env:TEMP "alloy-installer-windows-amd64.exe" $ConfigDst = "C:\Program Files\GrafanaLabs\Alloy\config.alloy" # Check if Alloy is already installed $AlloyService = Get-Service -Name "Alloy" -ErrorAction SilentlyContinue if ($AlloyService) { Write-Host "Grafana Alloy is already installed. Skipping installation." } else { # Download installer try { Write-Host "Downloading Grafana Alloy..." Invoke-WebRequest ` -Uri $AlloyUrl ` -OutFile $Installer ` -UseBasicParsing if (-not (Test-Path $Installer)) { throw "Alloy installer was not downloaded." } if ((Get-Item $Installer).Length -eq 0) { throw "Downloaded Alloy installer is empty." } } catch { Write-Error "Failed to download Grafana Alloy. Aborting. $($_.Exception.Message)" exit 1 } # Install Alloy Write-Host "Installing Grafana Alloy..." $process = Start-Process ` -FilePath $Installer ` -ArgumentList "/S" ` -Wait ` -PassThru if ($process.ExitCode -ne 0) { Write-Error "Alloy installer failed with exit code $($process.ExitCode). Aborting." exit $process.ExitCode } Remove-Item $Installer -Force -ErrorAction SilentlyContinue # Verify service was created $AlloyService = Get-Service -Name "Alloy" -ErrorAction SilentlyContinue if (-not $AlloyService) { Write-Error "Installation completed but the Alloy service was not found. Aborting." exit 1 } } # Download configuration try { Write-Host "Downloading Alloy configuration..." $TempConfig = Join-Path $env:TEMP "alloy-config.alloy" Invoke-WebRequest ` -Uri $ConfigUrl ` -OutFile $TempConfig ` -UseBasicParsing if (-not (Test-Path $TempConfig)) { throw "Configuration was not downloaded." } if ((Get-Item $TempConfig).Length -eq 0) { throw "Downloaded configuration is empty." } Copy-Item $TempConfig $ConfigDst -Force Remove-Item $TempConfig -Force } catch { Write-Error "Failed to download/install Alloy configuration. Aborting. $($_.Exception.Message)" exit 1 } # Restart Alloy try { Write-Host "Restarting Alloy..." Restart-Service -Name "Alloy" -Force } catch { Write-Error "Failed to restart Alloy. $($_.Exception.Message)" exit 1 } Write-Host "" Write-Host "Alloy deployment complete." Get-Service -Name "Alloy"