PowerShell & API's: Invoke-WebRequest
Microsoft's PowerShell can be leveraged for so many tasks and is able to make routine and mundane processes faster and easier thanks to its various components. I began learning PowerShell when I started working at Blue Cross in February of 2018 after it was recommended by my coworker Trevor, and it's thanks to teachings from him, and my coworkers Dan & Tim, that I've been enabled to do so many different things with it.
One of the many useful tasks I learned to do with PowerShell is how to interact with API's in order to do a multitude of things. This article for today will cover an extremely simple and publicly available API from MAC Vendors that I think is a terrific first API for anyone that is unfamiliar with them.
Let’s say you want to find out who the manufacturer is for a MAC address that you have. Typically, you would visit a web service like MAC Vendors (MV) and put in the mac address like I have below.
Web Page: https://macvendors.com/
But what if you have ten MAC addresses? Or thirty? Or three hundred? The last thing you want to do is type in, or copy & paste, all of those mac addresses into a web page. This is where having the ability to script is huge, as you can instead leverage an API.
API: https://macvendors.com/api
As you can see below, MV has even provided an example of what formats they accept and how to actually make the API call, by sending a request to https://api.macvendors.com/MACADDRESSHERE. It’s extremely important to note here that you are limited to 1,000 requests per day in combination with 1 request per second. This means you’ll have to rate-limit your API call(s) if you are going to request information for more than one MAC Address, which can be done by using "Start-Sleep -Milliseconds 1001" after your call.
How to
Open up a new scripting pane in PowerShell ISE, Visual Studio Code, or whatever you use to script and follow along!
We’re going to be using Invoke-WebRequest to make the API call, and to avoid errors about being unable to create a secure TLS/SSL connection we're going to put the following into the first line of our script: [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
Next we'll write our web request by using the following: Invoke-WebRequest "https://api.macvendors.com/9c934e94ed4a”. Below is the output from this command, where you see a successful status code and the manufacturer stored in the content.
There’s a lot of information from that output, but the only thing I want is content because that's where the Manufacturer information is stored. So I’m instead going to make my request the following instead: (Invoke-WebRequest "https://api.macvendors.com/9c934e94ed4a").content
Now we have something that I like a lot more for other purposes if I need to further manipulate it later on, and this could be stored in a variable if I chose to do that instead.
Final remarks
In my attempt to keep thing as *basic* as possible for a pure introduction to API's and PowerShell, that's all! If you followed along then you are now able to retrieve manufacturer information using a MAC Address with the MacVendors API. I fully encourage people to look up other API's to become familiar with interacting with responses.
Please see below for what my script pane in Visual Studio Code looked like at the end of this.