Chapter 40. Working with the web

 

This chapter covers

  • Getting data from a website
  • Working with JSON
  • Working with REST services

Web protocols have given us incredible access to data. Whether you’re consuming an XML feed from an internal web service, scraping HTML pages from the public internet, or interacting with REST services on a partner extranet, web technologies help make it happen. PowerShell is well connected to these technologies, too, meaning you can use it as a tool for automating those interactions.

Note

This chapter in particular focuses on Windows PowerShell v3 and v4. Much of what we’re discussing can be performed in older versions, but the shell itself lacks the commands. Instead, you end up working with the raw .NET Framework to accomplish these tasks. Cmdlets are always easier than raw .NET code.

At first glance this may seem to be a developer-oriented topic, but the administration tools of many software products within our environments are increasingly exposed as web services. As an administrator, you need to know how to access those services. First, though, how do you access a simple website?

40.1. Getting data from the web

PowerShell’s Invoke-WebRequest command is designed to send an HTTP request to a web server and to download the results. For example, if you run Invoke-WebRequest -Uri http://manning.com, you’ll get back a result object. That object includes a number of useful properties:

40.2. Using web sessions

40.3. Working with web responses

40.4. Using REST services

40.5. Using SOAP web services

40.6. Just in case

40.7. Summary