liveBook · Manning

Invoke-RestMethod

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

Invoke-RestMethod is a PowerShell cmdlet that sends HTTP or HTTPS requests and converts response content into usable PowerShell objects.

Invoke-RestMethod sends HTTP or HTTPS requests and converts response content into PowerShell objects. Save the result to a variable, then process it like any other object instead of parsing raw output or creating custom classes. section 9.3

A request normally combines a URI, method, body, and headers. The URI identifies the resource and may carry parameters; the method describes the operation, the body commonly carries POST or PUT data, and headers provide authentication or content-type information. JSON and XML have native PowerShell support. section 9.3

Retrieve an external IP address

$IP = Invoke-RestMethod -Uri 'https://api.ipify.org?format=json' $IP.ip section 9.3

Use API documentation to determine the service’s URI patterns, methods, headers, authentication, and response conventions. REST is an architectural style rather than a protocol or standard, so implementations can define their own patterns. section 9.3

For broader system-report automation, retrieve external IP information with ipify, combine it with internal network data and performance counters, and write the collected objects into a Word report. section 9.5

$IP = Invoke-RestMethod -Uri 'https:/ /api.ipify.org?format=json'
$IP
ip
--
48.52.216.180

A minimal request-and-property-access example shows how JSON returned by an API becomes a directly usable PowerShell object. section 9.3

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains request components, response conversion, REST boundaries, and a minimal retrieval example, but omits detailed error handling and authentication implementation.
Examples
Demonstrates an ipify JSON request and uses the returned external IP in a generated Word system report.
What it assumes
Assumes familiarity with PowerShell variables and objects while defining REST request components and response formats.
What you can do after
Apply it to retrieve API data as PowerShell objects and incorporate that data into automation workflows and reports.
How it connects
Connects REST APIs with HTTP requests, JSON, XML, Invoke-WebRequest, PowerShell objects, and document automation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.