PowerShell How-To

Exploring the Trello API with PowerShell

Get the most out of the task management software with automation.

What some people don't realize is that PowerShell is not only a powerful tool in a system administrator's arsenal but can also be used by software developers or techie consumers at home to manage and manipulate not-so-ordinary IT applications. One of those instances is using PowerShell to interact with REST APIs.

A REST API or application programming interface is a front end to a particular software service. It's a standard way of giving developers a way to read and manipulate objects in that particular service. APIs give people a way to use a particular service in a programmatic way rather than using the traditional user interface that most are used to.

For example, let's take the service Trello. Trello is a service that allows you to create boards where you can add lists and add cards to various lists. Trello is used for many different purposes with the most common being by software developers to control the software development lifecycle through a Kanban-like process. Cards are tracked from origination all the way to a typical "Done" list, which then indicates that particular task is complete. Trello is a great service for general task management as well.

I've recently joined an organization that uses Trello and required me to perform a specific set of tasks on a regular basis. The workflow was always the same:  move the card to X list, take ownership of the card, make a note in the card with a specific format, upload a file, etc. It was a consistent standard way of maintaining the cards on the Trello board. This type of workflow is ripe for automation and is why I decided to create a Trello PowerShell module.

In order to use this module, you'll first need to sign up for an API key and a secret. This will give you the credentials needed to interact with the Trello API from PowerShell. Once you've got your API credentials set up, you can then begin interacting with the Trello API.

To interact with the Trello API requires communicating over HTTP. PowerShell comes standard with a handy cmdlet called Invoke-RestMethod. This takes out a lot of the traditional work of interacting with a REST API. This cmdlet is what's doing the heavy lifting under the covers of this PowerShell module.

To get started, you'll need to authenticate to Trello. You do this with your API key and secret. Once you've imported the module, you can use the Request-TrelloAccessToken function. This function uses your API key to make a call out to Trello to gather the token you need to interact with the API.

PS> $token = Request-TrelloAccessToken –ApiKey MYAPIKEY

Once you have the token, you'll then need to save it along with your API key and secret to your computer so that you don't have to recall it every time. I do this by providing a function that allows you to save the configuration into the registry.

PS> Set-TrelloConfiguration –ApiKey MYAPIKEY –Token $token

Once the configuration has been saved, you'll then retrieve the configuration and make it available to your current PowerShell session.

PS> Get-TrelloConfiguration

Now you're ready to interact with the API through a number of various functions. For example, if you want to find all of your Trello boards, you'd use the Get-TrelloBoard function.

PS> Get-TrelloBoard
[Click on image for larger view.]  Figure 1.

You'll see I've got a few boards setup. Perhaps I want to see all of the lists on the board Articles. I can simply specify the Name parameter on the Get-TrelloBoard function and directly pass that to Get-TrelloBoardList.

PS> Get-TrelloBoard -Name Articles | Get-TrelloBoardList
[Click on image for larger view.]  Figure 2.

Need to find the cards in those lists? Simply send the output of Get-TrelloBoard to Get-TrelloCard instead.

PS> Get-TrelloBoard -Name Articles | Get-TrelloCard

After playing with this module for a while, you'll see that you are able to manage many things with the Trello service. Although not all functionality is currently supported, the module is open to you for modification. I've tried to maintain a specific coding standard using Invoke-RestMethod so by following Trello's API reference and using Invoke-RestMethod you should be able to implement PowerShell support in any Trello API call you'd like.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular