Skip to content

PowerShell

Scheduled tasks, RMM script blocks, and ad-hoc admin scripts report runs with one function. Paste this into your script or module:

function Send-LumaTrackRun {
    param(
        [Parameter(Mandatory)] [string] $Automation,
        [ValidateSet("success", "failure", "skipped", "cancelled")] [string] $Status = "success",
        [int] $DurationSeconds = -1,
        [int] $Units = -1,
        [string] $ExternalId,
        [string] $FailureReason,
        [string] $BaseUrl = $env:LUMATRACK_URL,
        [string] $ApiKey = $env:LUMATRACK_KEY
    )
    $Body = @{ automation = $Automation; status = $Status; source = "powershell" }
    if ($DurationSeconds -ge 0) { $Body.duration_seconds = $DurationSeconds }
    if ($Units -ge 0)           { $Body.units = $Units }
    if ($ExternalId)            { $Body.external_id = $ExternalId }
    if ($FailureReason)         { $Body.failure_reason = $FailureReason }
    try {
        Invoke-RestMethod -Method Post -Uri "$BaseUrl/api/v1/runs" `
            -Headers @{ Authorization = "Bearer $ApiKey" } `
            -ContentType "application/json" -Body ($Body | ConvertTo-Json) `
            -TimeoutSec 15
    } catch {
        # Telemetry must never take down the job it measures.
        Write-Warning "LumaTrack report failed: $($_.Exception.Message)"
    }
}

And in the job:

$Timer = [System.Diagnostics.Stopwatch]::StartNew()
try {
    $Disabled = Disable-StaleADAccounts   # the actual work
    Send-LumaTrackRun -Automation "ad-account-cleanup" `
        -DurationSeconds $Timer.Elapsed.TotalSeconds `
        -Units $Disabled.Count `
        -ExternalId "$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd)"
} catch {
    Send-LumaTrackRun -Automation "ad-account-cleanup" -Status failure `
        -FailureReason $_.Exception.GetType().Name `
        -ExternalId "$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd)"
    throw
}

Notes, honestly:

  • Report the failure path (the catch above). Success-only reporting produces a number nobody should present.
  • ExternalId makes retries idempotent. Derive it from whatever your scheduler already has: task run id, ticket id, or a date stamp for daily jobs.
  • Units fits batch jobs (accounts disabled, mailboxes archived); value those automations per unit so a 200-account sweep books more than a 3-account one.
  • Store the API key in your RMM's credential store or as a protected environment variable, never in the script body.
  • Field reference: Recording runs.