PowerShell
The Zharp module reports what makes a script fail — the error, where, and the code that ran — and runs it as one span, for scheduled tasks, Azure Automation runbooks, deploy and backup scripts.
This SDK is new, and its API may still change before 1.0. Requires Windows PowerShell 5.1 or PowerShell 7.6 (Windows, Linux or macOS).
Pure PowerShell: no assemblies of its own, so it never clashes with another module's .NET libraries, and it runs the same on Windows PowerShell 5.1 — still what most Windows Servers run — and PowerShell 7. It sends to Zharp with Invoke-RestMethod, and a network failure never fails your script.
Install
Install-Module Zharp -Scope CurrentUser # or, with PSResourceGet Install-PSResource Zharp
Initialize
Set your key, from the app's settings in Zharp, in ZHARP_KEY, then:
Import-Module Zharp
Start-Zharp -Service 'nightly-backup' # the key from $env:ZHARP_KEY
Invoke-Zharp { # the whole script as one span
Copy-Database -Name shop
Send-Report
}Invoke-Zharp runs the block as one span: the script's name, its duration and how it ended. An error that ends the block is reported once, with its stack and the code that ran, then thrown on, so the script fails exactly as it would have. Without a key nothing is sent and the block runs as written. The app's setup page in Zharp shows these steps with the key, service and environment filled in.
Errors stop the block
Inside the block, $ErrorActionPreference is Stop: a cmdlet's error ends the block instead of scrolling past, which is how most script failures go unnoticed. Your own setting is back after the block. -KeepErrorAction leaves it as it is; then only what already ends the block is reported.
Without the block
For a script you'd rather not indent, one trap at the top does the reporting:
Import-Module Zharp
Start-Zharp -Service 'nightly-backup'
trap { $_ | Out-Zharp -Unhandled; break } # reports what ends the script, which still failsPowerShell has no hook for an error that ends a script, so the line is yours to add. -Unhandled sends at once, before the script stops.
Scheduled tasks
# once, in an elevated prompt: a variable every scheduled task sees
[Environment]::SetEnvironmentVariable('ZHARP_KEY', 'zpk_…', 'Machine')
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -File C:\jobs\backup.ps1'
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -TaskName 'nightly-backup' -Action $action -Trigger $trigger -User 'SYSTEM'A failed run is reported before the task ends; the task's history shows the failure as before.
Azure Automation
Import Zharp into the Automation account (Shared Resources → Modules → Browse from gallery) for your runbook's runtime, and keep the key in an encrypted variable named ZharpKey:
# Runbook (5.1 or 7.x runtime), with Zharp imported from the gallery into the Automation account
Import-Module Zharp
Start-Zharp -Key (Get-AutomationVariable -Name 'ZharpKey') -Service 'rotate-certificates'
Invoke-Zharp {
Update-Certificates
}Cron job monitors
A Cron job monitor alerts when a job fails or never runs. With -Monitor and the token from the monitor's ping URL, the block pings it when it starts and when it ends, with how it ended and the run's trace: the monitor's run links to the error.
Invoke-Zharp -Monitor 'YOUR_MONITOR_TOKEN' {
Copy-Database -Name shop
}
# or once for the script: Start-Zharp -Monitor 'YOUR_MONITOR_TOKEN', or $env:ZHARP_MONITOREnd the block with return or throw rather than exit: PowerShell doesn't tell a module the code a block exited with, so after exit no finish ping is sent and the monitor goes by its schedule.
Logs
Off by default. With ZHARP_LOGS=true, the block's Warning, Error, Information and Verbose streams and Write-Host are sent with the trace, and still shown as before. Output is left alone: it's the script's data.
$env:ZHARP_LOGS = 'true'
Invoke-Zharp {
Write-Warning 'disk 91% full' # sent, and shown as before
Write-Host 'copied 120 files' # sent too; Output (the script's data) is left alone
}Write-ZharpLog is always sent. For a script that writes its own log file the Zharp agent reads, -Stamp returns the line ending in trace_id=… span_id=… instead, and the agent links it to the run.
Write-ZharpLog 'copied 120 files' -Attributes @{ files = 120 } # sent, whatever ZHARP_LOGS says
Write-ZharpLog 'copied 120 files' -Stamp | Add-Content C:\logs\backup.log # "copied 120 files trace_id=… span_id=…"Logs only
If you only want logs, turn tracing off. Logs, errors, and the user and session they belong to are still sent; no spans are recorded and no trace headers are added to outgoing requests. A request from a Zharp front end is still read, so its logs keep the caller's user, session and trace ID.
$env:ZHARP_TRACING = 'false'
Import-Module Zharp
Start-Zharp -Key 'zpk_…' -Service 'nightly-backup' -Logs
Invoke-Zharp { ./backup.ps1 }Or leave the code as it is and set ZHARP_TRACING=false ZHARP_LOGS=true in the environment.
The logs show on the Logs page under the service name, and on the app's Logs tab with the user and session of each line. The key comes from an app — see Get a key. To add tracing later, remove the option; nothing else changes.
Errors
Each error is reported once, with the .NET exception's type (the exception a method threw, for [IO.File]::ReadAllText; RuntimeException for throw "text"), its message, PowerShell's own stack trace and the column, and its FullyQualifiedErrorId and category:
System.Management.Automation.ItemNotFoundException: Cannot find path 'D:\backups\shop' because it does not exist. at Copy-Database, C:\jobs\backup.ps1: line 13 at Invoke-Backup, C:\jobs\backup.ps1: line 22 at <ScriptBlock>, C:\jobs\backup.ps1: line 40
With it goes the code that ran: the lines around the innermost five frames, read from your script files. Functions from modules in $env:PSModulePath aren't your code, so they don't decide where an issue is. Report an error you handled yourself:
try {
Sync-Files
} catch {
$_ | Out-Zharp -Attributes @{ share = '\\fs01\data' } # reported as handled, in this run's trace
}Failed web calls
A failed Invoke-RestMethod or Invoke-WebRequest carries its URL (secret-looking query values masked), method and status code.
Failed queries
A failed Invoke-Sqlcmd (or dbatools' Invoke-DbaQuery, or a SqlException) carries the query with its values taken out, the database, the server and the error number.
Invoke-Zharp {
Invoke-Sqlcmd -ServerInstance db1 -Database shop -Query "DELETE FROM carts WHERE created < '2026-01-01'"
}
# the error carries db.query.text "DELETE FROM carts WHERE created < ?", the database, the server and SQL Server's error numberReadable stack traces
Nothing to upload: the stacks this SDK sends already name your functions, files and lines, and issues are grouped by them.
Tracing
Inside the block $env:TRACEPARENT names its span, so a dotnet, node or python program it starts with a Zharp SDK continues the same trace. A script started inside a trace continues it in turn. Web calls carry it as a traceparent header through Invoke-ZharpRestMethod, or for the plain cmdlets with -TraceHttp.
Invoke-Zharp {
dotnet .\Exporter.dll # continues the trace (TRACEPARENT)
Invoke-ZharpRestMethod https://api.example.com/sync -Method Post # a span, and a traceparent header
}
Invoke-Zharp -TraceHttp {
Invoke-RestMethod https://api.example.com/sync -Method Post # the plain cmdlets get the header too
}Steps of their own:
$span = Start-ZharpSpan 'copy files' -Attributes @{ files = $files.Count }
Copy-Item $files -Destination $target
Stop-ZharpSpan $spanUser context
Set-ZharpUser -Id $ticket.RequesterId -Email $ticket.Requester Set-ZharpTag 'tenant' 'acme'
The API
Start-Zharp -Service 'backup' # settings; the rest from ZHARP_* variables
Invoke-Zharp { … } # a block as a span; reports and rethrows what ends it
$_ | Out-Zharp # an error you caught, as handled
Start-ZharpSpan 'step' / Stop-ZharpSpan $span # a span around some work
Set-ZharpUser -Id 42 -Email ops@example.com # on spans, logs and errors from here on
Set-ZharpTag 'tenant' 'acme' # likewise
Write-ZharpLog 'message' [-Level Warn] [-Stamp]
Invoke-ZharpRestMethod / Invoke-ZharpWebRequest # the web cmdlets with the trace
Send-Zharp # send what is queued nowOptions
| Start-Zharp | Environment | Default |
|---|---|---|
-Key | ZHARP_KEY | — nothing is sent without one |
-Service | ZHARP_SERVICE, OTEL_SERVICE_NAME | the script's file name |
-Environment | ZHARP_ENVIRONMENT | — |
-Release | ZHARP_RELEASE | — |
-Endpoint | ZHARP_ENDPOINT | https://api.zharp.io/api/v1/otlp |
-Monitor | ZHARP_MONITOR | — a Cron job monitor's token or ping URL |
-Logs | ZHARP_LOGS | false. See Logs. |
-NoSourceContext | ZHARP_CAPTURE_SOURCE_CONTEXT=false | the code that ran is sent |
| — | ZHARP_TRACING=false | errors and logs only |
| — | ZHARP_DISABLED=true | turns everything off |
| — | ZHARP_DEBUG=true | says on stderr when something can't be sent |
Invoke-Zharptakes-Name(the span's name),-Monitor,-Attributes,-KeepErrorActionand-TraceHttp.- What is queued is sent when the outermost block ends, when an error ends it, and when PowerShell exits — two seconds at most.
Supported versions
- Windows PowerShell 5.1 and PowerShell 7.6 (LTS); Windows, Linux and macOS.
- Locked-down servers with an
AllSignedexecution policy need a signed module; ask us.
Troubleshooting
Nothing arrives
On the app's setup page (Applications → the app → settings → Setup instructions), the verify step shows the first event as soon as it lands, and Send a test event checks the key on its own. If the test event arrives but your app's don't, the SDK isn't starting or isn't reaching Zharp:
- Is
initcalled, and before the code you expect to see? Without a key, the SDK starts nothing and says so once. - Does the service name match? With an app key everything goes to that app whatever it says; with a workspace key,
servicedecides the app — see Keys. - Data is sent in batches every few seconds. A process that exits straight away should call
flushfirst. - Logs and errors arrive but no traces? Check that tracing isn't turned off — see Logs only.
- Is the script's work inside
Invoke-Zharp(or is thetrapline there)? Errors outside it aren't seen. - A scheduled task runs as another user: is
ZHARP_KEYa machine variable, or passed with-Key? ZHARP_DEBUG=trueprints why something couldn't be sent (a proxy, TLS, a firewall).
401 or 403
Ingest answers 401 when the key is missing, mistyped, revoked or rolled, and 403 when the request comes from an origin or IP address the key doesn't allow. Neither is retried: the SDK warns once and drops what was refused, and it never throws into your code. Copy the key again from the app's settings.