Automation · PowerShell Automation
REST APIs
Cheat sheet · https://pcornel.dk/topic.php?topic=5250
Answer
PowerShell calls REST APIs with Invoke-RestMethod, which converts JSON responses to objects, supports headers, bodies, authentication and, in PowerShell 7, retries with -MaximumRetryCount.
Remember
- Invoke-RestMethod parses JSON responses into objects.
- Pass headers and body explicitly.
- PowerShell 7 supports retries.
First checks
Invoke-RestMethod -Uri <api> -Headers @{Authorization="Bearer $t"} -TimeoutSec 30— REST call- -StatusCodeVariable / try catch for errors — handling
- Pagination loops — completeness
Verify in this order
request → API → JSON → objects
Where it usually breaks
Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload.
REST APIs · PowerShell operational reference
| Boundary | REST APIs reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5250