Skip to main content

PowerShell

Goal

Write a PowerShell block that reads nested JSON and publishes one compact line. Time: 10 minutes. Needs: pwsh.

language: powershell
$ErrorActionPreference = 'Stop'
if ([string]::IsNullOrWhiteSpace($env:TERMORQ_INPUT)) {
    throw 'TERMORQ_INPUT is missing'
}
$data = Get-Content -Raw -LiteralPath $env:TERMORQ_INPUT | ConvertFrom-Json
$text = $data.trigger.text
if ($text -isnot [string]) {
    throw 'trigger.text must be a string'
}
@{ message = $text } | ConvertTo-Json -Compress -Depth 10

Things to know

  • Files run with -NoProfile -NonInteractive -ExecutionPolicy Bypass, so your profile isn’t loaded.
  • -Raw reads the whole file. -LiteralPath stops PowerShell treating [ and * in the path as wildcards.
  • Always use -Compress. Without it, ConvertTo-Json prints several lines and breaks the last-line rule. Add -Depth for nested objects.
  • $ErrorActionPreference = 'Stop' turns non-terminating errors into failures.
  • Captured Command Prompt commands become PowerShell blocks that call cmd.exe through %ComSpec%.
Check your run

Execute step prints {"message":"Hello from curl"}.

Exercise

Publish upper (the text in upper case) and length (number) in the same object.

Details
$data = Get-Content -Raw -LiteralPath $env:TERMORQ_INPUT | ConvertFrom-Json
$t = [string]$data.trigger.text
@{ upper = $t.ToUpperInvariant(); length = $t.Length } | ConvertTo-Json -Compress