Use this when PowerShell calls REST APIs.
REST APIs
PowerShell calls REST APIs with Invoke-RestMethod, which converts JSON responses to objects, supports headers, bodies, authentication and, in PowerShell 7, retries with -MaximumRetryCount.
1. Invoke-RestMethod parses JSON into objects.
2. Set timeouts.
3. Handle errors and status codes.
4. Paginate.
5. Don't skip certificate checks in production.
request → API → JSON → objects
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Start with a read-only request, capture status plus key headers and inspect the returned object structure. For changes, retain the server's resource/operation identifier and perform a follow-up GET or status request until the documented terminal state is reached. Verify paging so a successful first page is not mistaken for a complete inventory.
Step 2 Common mistakes Narrow the fault
Invoke-RestMethodwithout error handling.
Step 3 Where it fails Narrow the fault
DNS/TLS/network failure, HTTP authentication, API authorization, schema validation, rate limiting and eventual target convergence are distinct boundaries. A parsed response is not evidence the full collection was retrieved or a mutation converged. Keep raw protocol facts and PowerShell object interpretation separately observable.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Invoke-RestMethod parses JSON responses into objects.
- Pass headers and body explicitly.
- PowerShell 7 supports retries.
Invoke-RestMethod -Uri <api> -Headers @{Authorization="Bearer $t"} -TimeoutSec 30— REST call- -StatusCodeVariable / try catch for errors — handling
- Pagination loops — completeness
ReferenceWhy it matters1 section · depth on demand
In depth
Invoke-RestMethod calls REST APIs and converts JSON responses into PowerShell objects automatically; Invoke-WebRequest returns the raw response with status code and headers when you need them.
$h = @{ Authorization = "Token $token"; Accept = 'application/json' }
$devices = Invoke-RestMethod -Uri "$nb/api/dcim/devices/?site=oslo&limit=1000" -Headers $h
$devices.results | Select-Object name, @{n='ip';e={$_.primary_ip4.address}}
$body = @{ vid = 30; name = 'VOICE'; site = @{ slug = 'oslo' } } | ConvertTo-Json -Depth 5
Invoke-RestMethod -Method Post -Uri "$nb/api/ipam/vlans/" -Headers $h -ContentType 'application/json' -Body $body
PowerShell 7 adds -FollowRelLink for paginated APIs, -StatusCodeVariable, -MaximumRetryCount and -RetryIntervalSec. Set -Depth on ConvertTo-Json — the default (2) silently truncates nested objects.
ReferenceImportant terms & boundaries1 section · depth on demand
PowerShell mechanics
Invoke-RestMethod turns HTTP requests and many JSON/XML responses into PowerShell-friendly data, but the authoritative protocol boundary remains HTTP: URI, method, headers, authentication, status code, content type, body and server-side resource semantics. Automatic deserialization is convenient yet can hide raw-response details needed for pagination, rate limits, correlation IDs or vendor error contracts.
ReferenceConfiguration & state1 section · depth on demand
Authoring and lifecycle
Model API calls with explicit base URI/version, authentication acquisition, timeout/retry rules and idempotency semantics. Serialize request bodies intentionally with the correct content type, protect tokens from logs, and respect server rate-limit/concurrency guidance. Separate transport retry from business retry so non-idempotent operations are not duplicated blindly.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Configuration & expected output
Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.
Configuration example
Assumptions: Review target identity, permissions and environment before mutation.
$headers = @{ Authorization = "Bearer $token" }
$result = Invoke-RestMethod -Uri $uri -Headers $headers -Method GetVerification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Microsoft Learn documentation reviewed for bounded PowerShell reference claims. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.5 · confidence 94%Quick lookup tables
REST APIs · PowerShell operational reference
Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.
| 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 |
Deep reference
Healthy vs suspicious evidence
Output interpretation
PowerShell may deserialize JSON/XML into objects while headers/status live elsewhere depending on cmdlet/version options. HTTP 2xx means the HTTP request was accepted/satisfied at that layer; APIs can still report asynchronous work or domain-specific partial results. For failures, preserve status code, safe response metadata and correlation ID rather than logging bearer tokens or secret payloads.