Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
languagepowershell
# job
# -agentpath .
# -servicename XYZ
# -collectorid SL_CovCollectorAgent (default)
# -logdir
# -loglevel
param (
#    [Parameter(Mandatory=$true)][string]$agentpath,
    [string]$agentpath,
    Parameter(Mandatory=$true)][string]$servicename,
    [string]$collectorid = "GlobalCollector",
    [string]$logdir,
    [ValidateSet(0,1,4,6,999)][string]$loglevel = 0
)

#Output here argv (similar to java agent)for 

# ================= 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)) ) {
    LogAndExitWrite-Output "[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"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.
# 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) ) 
    {
        LogAndExitWrite-Output "[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)) 
    {
        LogAndExitWrite-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)) {
    LogAndExitWrite-Output "[ERROR] 'servicename' parameter is not provided." $true
}

$ServicePathInRegistry = $(Join-Path -Path "HKLM:\System\CurrentControlSet\Services\" -ChildPath $servicename)

if( -Not $(Test-Path -Path $ServicePathInRegistry) ) {
    LogAndExitWrite-Output "[ERROR] '$servicename' not found in Registry (at 'HKLM:\System\CurrentControlSet\Services\')" 
} else {
    LogAndExitWrite-Output "[OK] Registry includeshas anvalid entry for service '$servicename'.at $ServicePathInRegistry"
}

Write-Output "`n***************************`n"
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