[CmdletBinding()] param( [string]$WebhookUrl = "", [string]$SecretValue = "" ) $ErrorActionPreference = "Stop" function Get-NinjaValue { param([Parameter(Mandatory)][string]$Name) $value = [Environment]::GetEnvironmentVariable($Name, "Process") return if ($null -eq $value) { "" } else { $value.Trim() } } if ([string]::IsNullOrWhiteSpace($WebhookUrl)) { $WebhookUrl = Get-NinjaValue -Name "webhookurl" } if ([string]::IsNullOrWhiteSpace($SecretValue)) { $SecretValue = Get-NinjaValue -Name "secretvalue" } if ([string]::IsNullOrWhiteSpace($WebhookUrl) -or [string]::IsNullOrWhiteSpace($SecretValue)) { throw "WebhookUrl and SecretValue must be supplied as NinjaOne script variables." } $installRoot = Join-Path $env:ProgramFiles "OCSentinel" $configPath = Join-Path $installRoot "config\ocsentinel-client.json" $secretScript = Join-Path $installRoot "scripts\protect-ocsentinel-secret.ps1" $monitorScript = Join-Path $installRoot "scripts\run-ocsentinel-monitor.ps1" $secretPath = "C:\ProgramData\OCSentinel\secrets\ocsentinel-upload-secret.dat" foreach ($path in @($configPath, $secretScript, $monitorScript)) { if (-not (Test-Path -LiteralPath $path)) { throw "OCSentinel installation is incomplete. Missing: $path" } } $config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json $config.n8nWebhookUrl = $WebhookUrl $config.environment = "production" $config | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding UTF8 Write-Host "OCSentinel upload endpoint configured." & powershell.exe -NoProfile -ExecutionPolicy Bypass -File $secretScript -SecretValue $SecretValue if ($LASTEXITCODE -ne 0) { throw "Writing the protected upload secret failed with code $LASTEXITCODE" } Write-Host "Running signed OCSentinel test scan and upload." & powershell.exe -NoProfile -ExecutionPolicy Bypass -File $monitorScript ` -Mode status ` -ClientConfigPath $configPath ` -SecretPath $secretPath ` -UploadMode required exit $LASTEXITCODE