# Automating CollectorService Deployment

## Problem <a href="#problem" id="problem"></a>

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 <a href="#solution" id="solution"></a>

{% hint style="warning" %}
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.
{% endhint %}

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.

{% columns %}
{% column %}
Sample command to use:

{% code overflow="wrap" lineNumbers="true" %}

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

{% endcode %}
{% endcolumn %}

{% column %}

<figure><img src="/files/oFEcL430t9SC2zRHUEeF" alt=""><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

Script

```powershell
# 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 
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sealights.io/knowledgebase/setup-and-configuration/troubleshooting-faq/.net-.netcore/automating-collectorservice-deployment.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
