Building PowerShell Tools for MSPs: Working with REST APIs

This post is part of a series on PowerShell for MSPs.

Using SFTP

HTML Tables for Reporting

How to Encrypt Passwords in PowerShell

As a Managed Service Provider, being able to automate features and functions of company applications can severely cut downtime and costs. PowerShell can be used to do this. Some applications are now starting to come out with PowerShell Cmdlets that let users automate their product. This is awesome when Vendors take the time to do this. However, some Vendors are not at this stage yet and that’s when it’s a little more difficult to use PowerShell to get what you need. In this situation, we sometimes have to use Rest APIs.

Rest APIs are APIs that use HTTP to perform requests and receive responses. They typically use the 4 following methods:

  1. Get – Retrieves a resource
  2. PUT – Modifies or updates a resource
  3. POST – Creates a resource
  4. Delete- Removes a resource

Most applications have some sort of Restful API documentation that can help with understanding what types of tasks can be accomplished. It is recommended to look around online or ask the vendor for this documentation and look over it. Lets use Open Weather Map API for an example on how to use a Restful API and PowerShell together. Open Weather Map has some great documentation on how to use their REST API posted here.

Lets say we want to use the API to find out the 5 day forecast in Chicago, Illinois, US.  Looking through the documentation we can see they have a section here on how to use the API to get this information. Said info provides us the URL we can use to execute the search via HTTP. The documentation gives us the URL:{city name},{country code}

They also provide documentation on the different parameters we can specify to get the information we want. This is why it is extremely useful to review the REST API documentation. Each Vendor is going to have their own way of searching information with their REST APIs. However, after digging my way through the documentation and registering for an API key, I was able to come up with the correct URL to look up the 5 day forecast of Chicago:

Since the REST API uses HTTP as the protocol to transfer information. I always like to test this by pasting the URL in a web browser. Below you can see if I paste the URL in, I get my 5-day forecast of the weather in Chicago:

Test REST API through HTTP protocol.

Now, how do we use this with PowerShell? Easy. We use invoke-restmethod to access our query over http. This will allow us to manipulate the retrieved data with PowerShell and use it for automation. Below is the syntax we will use:

invoke-restmethod -Uri -Method Get

Since we are just retrieving data with the API we are using a GET method, and the URI is going to be our URL that we tested above. After running the command we get the following information displayed:

Running the GET command to retrieve data.

As you can see the information is the same as what we were seeing in our browser, except that the formatting is different. This is where learning how to display properties and XML information in PowerShell comes in handy. What I do is capture the info in a variable like below:

$data = invoke-restmethod -Uri "" -Method Get

Now I can use the variable to search through each property subset and find the info I want to see:


Data list during REST API test

It can take some time and it’s sort of more of an art than a science, but in the end I can get a quick and dirty view of the weather description and time with the following syntax:

$data.list | select dt_txt,weather

REST API return view

If we wanted to take it a step further, we could parse out the information and format it into a beautiful HTML table, but that is beyond the scope of this article.

This is obviously a very basic use of using PowerShell with a REST API, there is so much more that can be done such as PUT and DELETE calls depending on the REST API. Knowing how to look up the documentation and figure out how to obtain the information you need is a huge skillset and can have huge potential gains for you and your company.

Good luck! Let me know in the comments on some of the ways you’ve used PowerShell with a rest API. Some of the applications I’ve successfully automated with PowerShell and the Rest API where: MacAfee Endpoint Protection, Solarwinds, GitLab, Big Fix Patching, GitHub, and various websites.

More articles about how MSPs can make the most out of PowerShell in their operations:

Using SFTP

HTML Tables for Reporting

How to Encrypt Passwords in PowerShell

Hungry for More Automation for MSPs Tips?

Watch our on-demand webinar 4 Ways to Improve your MSP by Embracing Automation and DevOps hosted by Microsoft Cloud and Datacenter Management MVPs Andy Syrewicze and Adam “The Automator” Bertram. The webinar covers:

  • Simple ways to get started with automation
  • How PowerShell can help save time and money when used for lengthy tasks
  • How treating your scripts like code can prevent mistakes and costly problems
  • How Leveraging REST APIs can enable further automation and operational efficiencies

4 Ways to Improve your MSP by Embracing Automation and DevOps

Altaro O365 Backup for MSPs
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

47 thoughts on "Building PowerShell Tools for MSPs: Working with REST APIs"

Leave a comment

Your email address will not be published. Required fields are marked *