Write a correlation-aware JSON operation log
Documentation: Microsoft Learn
Each line is a complete JSON object with an ISO-8601 UTC timestamp and one correlation ID.
The script
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
$correlationId = [guid]::NewGuid().Guid
$log = '.\operation.jsonl'
function Add-OperationLog {
[CmdletBinding()]
param([string]$Level, [string]$Message, [hashtable]$Data = @{})
[pscustomobject]@{ Timestamp = (Get-Date).ToUniversalTime().ToString('o');CorrelationId = $correlationId;Level = $Level;Message = $Message;Data = $Data } |
ConvertTo-Json -Compress -Depth 6 | Add-Content -LiteralPath $log -Encoding utf8 }
Add-OperationLog -Level Information -Message 'Inventory started' -Data @{ Computer = $env:COMPUTERNAME }- Stage 1$correlationId
Positional values or no named parameters
How it works
Each line is a complete JSON object with an ISO-8601 UTC timestamp and one correlation ID. JSON Lines keeps appends cheap and lets a collector process records one at a time. The Data field preserves task-specific context without changing the top-level schema.
Adapting it
Add operation name, script version and duration when those are useful across runs. Send the same object to your logging API instead of Add-Content when a central collector exists.