.Net - Automate CollectorService deployment for multiple Windows Services

Problem

Current instructions for deploying the Sealights CollectorService are manual, and we have several services deployed on our QA machines. We want to automate this step.

Solution

The sample code listed in this article is not supported by Sealights as an official plugin, agent, or product and is shared as a courtesy.

Please find below a sample script allowing you to register Sealights Collector Service with your service using a single command.
This script will check the agent paths, enable/disable logs, and update the registry for a specified service.

 

Sample command to use:

.\SL.RegisterServiceProfiler.ps1 -agentpath C:\Sealights\SL.DotNet -servicename MySealightsTest -logdir C:\Sealights\logs -loglevel 6

Script

# job # -agentpath . # -servicename XYZ # -logdir # -loglevel param ( [string]$agentpath, [Parameter(Mandatory=$true)][string]$servicename, [string]$logdir, [ValidateSet(0,1,4,6,999)][string]$loglevel = 0 ) #Output here argv for # ================= BEGIN MAIN SCRIPT ================= Write-Output "" #Write Powershell version Write-Output "Current Powershell Version: $($PSVersionTable.PSVersion.ToString())" #If no agent path is provided stop. if ( ([string]::IsNullOrWhiteSpace($agentpath)) ) { Write-Output "[ERROR] No agent provided." exit } #Test Agent Path and output its version try{ $SLAgentPath=$(Resolve-Path -Path $agentpath -ErrorAction SilentlyContinue).Path } catch { $SLAgentPath=$agentpath } $SlAgentVersionPath = $(Join-Path -Path $agentpath -ChildPath "version.txt") if( $(Test-Path -Path $SlAgentVersionPath -PathType Leaf) ) { Write-Output "Sealights Agent version is: $(Get-Content $SlAgentVersionPath)" } Write-Output "" $COR_ENABLE_PROFILING=$CORECLR_ENABLE_PROFILING=1 # In old agent versions, variable name was "Sealights_CollectorId" $SL_CollectorId="GlobalCollector" $COR_PROFILER="{01CA2C22-DC03-4FF5-8350-59E32A3536BA}" $COR_PROFILER_PATH_32=$(Join-Path -Path $agentpath -ChildPath "x86\SL.DotNet.ProfilerLib_x86.dll") $COR_PROFILER_PATH_64=$(Join-Path -Path $agentpath -ChildPath "x64\SL.DotNet.ProfilerLib_x64.dll") # Separate 32/64-bit profiler paths are only supported for .NET frameworks 4.6+ # For older versions, only a single COR_PROFILER_PATH variable is supported. # Core $CORECLR_PROFILER=$COR_PROFILER $CORECLR_PROFILER_PATH_32=$COR_PROFILER_PATH_32 $CORECLR_PROFILER_PATH_64=$COR_PROFILER_PATH_64 #Check Agent DLL files are located in provided path @($COR_PROFILER_PATH_64, $COR_PROFILER_PATH_32).ForEach({ if( -Not $(Test-Path -Path $_ -PathType Leaf) ) { Write-Output "[ERROR] $_ does not exist." } }) if (-Not [string]::IsNullOrWhiteSpace($logdir)) { if (-Not $(Test-Path -Path $logdir)) { Write-Output "[Warning] Folder $logdir does not exist." } $SL_LogDir=$logdir } if ($loglevel -ne 0) { $SL_LogLevel=$loglevel } Write-Output "`n**** Variables for copy ***`n" $multistring=@() Get-Variable *Cor_*,*CLR_*PROF*,*SL_*,*Sealights_* | ForEach-Object { Write-Output "$($_.name)=$($_.value)" $multistring+="$($_.name)=$($_.value)" } Write-Output "`n***************************`n" #When target is Registry, validate servicename exists in local registry if ([string]::IsNullOrWhiteSpace($servicename)) { Write-Output "[ERROR] 'servicename' parameter is not provided." $true } $ServicePathInRegistry = $(Join-Path -Path "HKLM:\System\CurrentControlSet\Services\" -ChildPath $servicename) if( -Not $(Test-Path -Path $ServicePathInRegistry) ) { Write-Output "[ERROR] '$servicename' not found in Registry (at 'HKLM:\System\CurrentControlSet\Services\')" } else { Write-Output "[OK] Registry has valid entry at $ServicePathInRegistry" } try { $ret = New-ItemProperty -Force -Path $ServicePathInRegistry -Name 'Environment' -PropertyType MultiString -Value $multistring Write-Output "[OK] Registry updated Succesfully for '$servicename'`n" } catch { Write-Output "[ERROR] Cannot update Registry." } # End Of Script