Catalyst / admin/Synapse-Sonar 14.8 GB / 57.8 GB 40.0 GB free
Help Sign in

admin / Synapse-Sonar

public

Attack Surface Simulation

Code Issues Pull requests Pipelines Packages Security Insights Wiki Settings
Synapse-Sonar / synapse-sonar / public / agents / windows-agent / install.ps1 2184 B · main
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
  Install the Synapse Sonar host agent on Windows.
  Run from an elevated PowerShell:  powershell -ExecutionPolicy Bypass -File .\install.ps1
  Registers a scheduled task that runs the agent at startup as SYSTEM.
#>
#Requires -RunAsAdministrator
$ErrorActionPreference = 'Stop'

$src      = Split-Path -Parent $MyInvocation.MyCommand.Path
$dest     = Join-Path $env:ProgramFiles 'SonarAgent'
$dataDir  = Join-Path $env:ProgramData 'SonarAgent'
$taskName = 'SynapseSonarAgent'

Write-Host "==> Installing agent to $dest"
New-Item -ItemType Directory -Force -Path $dest    | Out-Null
New-Item -ItemType Directory -Force -Path $dataDir | Out-Null
Copy-Item (Join-Path $src 'sonar-agent.ps1') (Join-Path $dest 'sonar-agent.ps1') -Force

$cfgPath = Join-Path $dataDir 'config.json'
if (-not (Test-Path $cfgPath)) {
  Copy-Item (Join-Path $src 'sonar-agent.config.example.json') $cfgPath
  Write-Host "    -> created $cfgPath (EDIT THIS: SonarUrl + IngestKey)"
} else {
  Write-Host "    -> $cfgPath already exists, leaving as-is"
}

Write-Host "==> Registering scheduled task '$taskName'"
$psExe = (Get-Command powershell.exe).Source
$action = New-ScheduledTaskAction -Execute $psExe `
  -Argument "-ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -File `"$dest\sonar-agent.ps1`""
$trigger   = New-ScheduledTaskTrigger -AtStartup
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
$settings  = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
  -StartWhenAvailable -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) `
  -ExecutionTimeLimit ([TimeSpan]::Zero)

Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger `
  -Principal $principal -Settings $settings -Force | Out-Null
Start-ScheduledTask -TaskName $taskName

Write-Host ""
Write-Host "Done. The agent is running and will start at every boot."
Write-Host "  Config: $cfgPath"
Write-Host "  Logs:   $dataDir\sonar-agent.log"
Write-Host "  Manage: Get-ScheduledTask $taskName | Stop-ScheduledTask / Start-ScheduledTask"
Write-Host "  Remove: Unregister-ScheduledTask -TaskName $taskName -Confirm:`$false"