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
Note |
---|
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
Code Block | ||
---|---|---|
| ||
# job
# -agentpath .
# -servicename XYZ
# -collectorid SL_CovCollectorAgent (default)
# -logdir
# -loglevel
param (
# [Parameter(Mandatory=$true)][string]$agentpath,
[string]$agentpath,
[string]$servicename,
[string]$collectorid = "GlobalCollector",
[string]$logdir,
[ValidateSet(0,1,4,6,999)][string]$loglevel = 0
)
#Output here argv (similar to java agent)
# ================= FUNCTIONS DEFINITIONS =================
function LogAndExit( [string]$MyMsg, [switch]$ForceExit) {
Write-Output $MyMsg
#Write-Output "ForceExit = $ForceExit"
# if ($ForceExit) {exit(1)}
}
# ================= 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)) ) {
LogAndExit "[ERROR] No agent provided. Exiting" $true
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=$collectorid
$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.
# NOTE: When we deprecate support for 4.5/4.5.1/4.5.2, we can remove setting 'COR_PROFILER_PATH' entirely.
#$Cor_Profiler_Path="$COR_PROFILER_PATH_64"
# 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) )
{
LogAndExit "[ERROR] $_ does not exist."
#LogAndExit "[ERROR] $_ does not exist. Please check the SL Agent installation path."
#LogAndExit(('my string').ToString(),"$true")
}
})
if (-Not [string]::IsNullOrWhiteSpace($logdir)) {
if (-Not $(Test-Path -Path $logdir))
{
LogAndExit "[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)) {
LogAndExit "[ERROR] 'servicename' parameter is not provided." $true
}
$ServicePathInRegistry = $(Join-Path -Path "HKLM:\System\CurrentControlSet\Services\" -ChildPath $servicename)
if( -Not $(Test-Path -Path $ServicePathInRegistry) ) {
LogAndExit "[ERROR] '$servicename' not found in Registry (at 'HKLM:\System\CurrentControlSet\Services\')"
} else {
LogAndExit "[OK] Registry includes an entry for service '$servicename'."
}
Write-Output "`n***************************`n"
New-ItemProperty -Force -Path $ServicePathInRegistry -Name 'Environment' -PropertyType MultiString -Value $multistring
# End Of Script |