In a post from last year, Steven Swenson explained how to use the Invoke-RestMethod cmdlet in PowerShell to query the Cloud Office REST API.
Since that time, I published Invoke-RsCloudOfficeRequest, a PowerShell module that takes all the work out of interacting with the Rackspace Cloud Office REST API. It handles:
- Passing the authentication header
- Encoding PowerShell input into the expected body format
- Parsing error responses into a meaningful format
- Unpaginating paged responses
- Storing API credentials so you don’t have to type them in every time (Optional)
But enough talk, let’s learn how to use it. Continue reading
Share and Enjoy
Many don’t know that the Cloud Office Control Panel also has a REST API. You can find the API documentation here. Most of the functionality available in the web control panel is also available in the REST API. In this blog post I’m going to walk through some PowerShell code that accesses the API.
In previous versions of PowerShell, users had to rely on the .NET framework and other secondary resources to use a REST API. However, v3 of Powershell includes a new cmdlet called Invoke-RestMethod. There were some limitations in v3 specific to how you set headers, but v4 of PowerShell allows you to have full control over your request. Be sure you’re using PowerShell v4 to run the examples below.
Of course the first thing we have to worry about is auth. The REST API requires an X-Api-Signature header, which is compiled by the Get-XApiSignature function. That function requires your api key, secret, and the value of the User-Agent header.
The next file, ListDomains.ps1, gets the XApiSignature header value, builds a hash for all the headers, and then makes the call to Invoke-RestMethod to retrieve a list of domains from the API. Invoke-RestMethod also parses the body of the response into an object (similar to how web-api works and is a huge time-saver!) This allows us to pipe the result directly into Format-Table to display the list of domains in a readable format.
In part two we’ll get fancy and create a mailbox.
Share and Enjoy