.Net - How to ensure I am running the latest/recommended agent version

Problem

When configuring the SeaLights agent for DotNet, it isn't easy to manage the agent files in the correct version - especially in standalone environments (containers, backend servers, etc)

Solution

The following PowerShell script checks for the recommended agent and latest version, and in case it does not exist locally - downloads & unzips it in the given directory.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $previousProgressPref=$global:ProgressPreference $global:ProgressPreference = "SilentlyContinue" #Disable Progress bar to speed up download and expand $sldomain="mycompany.sealights.co" #Update with your customer domain $slToken="$(Get-Content sltoken.txt)" #Prefer a credential Environment variable for the token Write-Output "Retrieve version from Sealights settings..." $slHeader=@{'Accept'='application/json'; 'Authorization'="Bearer $slToken"} $SLSettingsVersion = ((iwr -Uri https://$sldomain/api/v2/agents/dotnet/recommended -UseBasicParsing -Headers $slHeader).Content | ConvertFrom-Json | Select-Object agent).agent.version $SLAgentPath=".\SL.DotNet\" $SlAgentVersionPath = $(Join-Path -Path $SLAgentPath -ChildPath "version.txt") if( $(Test-Path -Path $SlAgentVersionPath -PathType Leaf) ) { $SLLocalVersion=Get-Content $SlAgentVersionPath if ( $SLLocalVersion -eq $SLSettingsVersion ) { Write-Output "Skipping Download - Local agent matches current settings version: $SLLocalVersion" } else { Write-Output "Local agent is not using the latest version. Deleting local agent." Remove-Item -Recurse -Force $SLAgentPath #Get-ChildItem -path $SLAgentPath -include * -Recurse -exclude sltoken.txt -erroraction silentlycontinue | Remove-Item -Force -Verbose } } if( -Not $(Test-Path -Path $SlAgentVersionPath -PathType Leaf) ) { Write-Output "Download Sealights DotNet agent set in settings..." iwr -OutFile .\sealights-dotnet-agent.zip -UseBasicParsing -Uri http://agents.sealights.co/SL.DotNet/SL.DotNet-$SLSettingsVersion.zip Write-Output "Expanding downloaded archive and placing token..." Expand-Archive .\sealights-dotnet-agent.zip -DestinationPath $SLAgentPath -Force Remove-Item -Force .\sealights-dotnet-agent.zip Get-ChildItem -Directory -Path .\SL.DotNet\ | Foreach-Object { $slToken | Out-File -NoNewline -Force (Join-Path $_.FullName "sltoken.txt") } Write-Output "Sealights agent version used is: $(Get-Content $SlAgentVersionPath)" } #Restore progress bar $global:ProgressPreference = $previousProgressPref #End of script