Using Wunderlist Module in Azure Automation–Part 2

 

In the first part of this blog post series about using Using Wunderlist Module in Azure Automation I explained how to get started with the PowerShell Wunderlist Module.

Please check this blog post first if you have not done so.

In this last blog post we are going to use the PowerShell Wunderlist Module within a Runbook in Azure Automation.

Azure Automation

Microsoft Azure Automation provides a way for users to automate the manual, long-running, error-prone, and frequently repeated tasks that are commonly performed in a cloud and enterprise environment.

Configure an Azure Automation Account

Before we can start with creating Runbooks we first need to create and configure an Azure Automation Account. Follow the steps described here: https://azure.microsoft.com/en-us/documentation/articles/automation-sec-configure-azure-runas-account/ if you have not created an Azure Automation Account yet.

If everything goes well you would see something similar as this in the (new) Azure Portal.

image

Azure Automation Assets

If we want to start using the PowerShell Wunderlist Module we need to create a number of Assets first. Click on the Assets Icon on the Azure Portal to add the following Assets:

  1. PowerShell Wunderlist Module
  2. Secure Variables for Wunderlist ClientID and AccessToken.

 

image

Import the PowerShell Wunderlist Module from the PowerShell Gallery.

Select Browse Gallery and search for the Wunderlist Module and Import the Module.

image

image

 

Configure the ClientID and AccessToken secure variables.

The PowerShell Wunderlist Module needs two variables to be configured. If you have already configured the PowerShell Wunderlist Module locally you can easily retrieve these values from there if you have not saved them already earlier.

Open PowerShell Console and import the Wunderlist module and run one of the Wunderlist Commands.

 
[sourcecode language='powershell' ]
Import-Module -Name Wunderlist

Get-WunderlistUser

Get-Variable -Name ClientID,AccessToken

 

image

You need to copy the values of these variables to the Azure Automation Secure Variables Assets.

Click on Add a variable in the Assets section.

image

Make sure you create two new Secure Variables called AccessToken and ClientID and store the values from your local PowerShell session.

 

Create a Wunderlist Runbook

We now have all prerequisites ready for creating a new Wunderlist Runbook. If you want to implement Source Control for Azure Automation Runbooks please take a look at the following blog post: https://blogs.technet.microsoft.com/stefan_stranger/2016/08/05/setting-up-source-control-for-azure-automation/

The goal is to create a new Wunderlist Task that help us reminding to stop an Azure Virtual Machine when this is running after 6:00 PM.

Open the PowerShell ISE and navigate the Github Repository where you want to store the Runbooks.

image

 

Create the Runbook:

image

 
[sourcecode language='powershell' ]
# ---------------------------------------------------
# Script: C:\Users\Stefan\Documents\GitHub\AzureAutomationDemo\Runbooks\Demos\AAWunderlistDemo.ps1
# Tags: Azure, AzureAutomation, Automation, PowerShell, Runbook
# Version: 0.1
# Author: Stefan Stranger
# Date: 08/08/2016 15:04:45
# Description: Azure Automation Runbook which checks for running VM and creates a Wunderlist Task if WM
#              is running.
# Comments:
# Changes:  
# Disclaimer: 
# This example is provided “AS IS” with no warranty expressed or implied. Run at your own risk. 
# **Always test in your lab first**  Do this at your own risk!! 
# The author will not be held responsible for any damage you incur when making these changes!
# ---------------------------------------------------

#region variables
$connectionName = "AzureRunAsConnection"
$ClientID = Get-AutomationVariable -Name "ClientID"
$AccessToken = Get-AutomationVariable -Name "AccessToken"
#endregion

#The PowerShell Wunderlist Module can use environment variables as input.
$Env:ClientID = $ClientID 
$Env:AccessToken = $AccessToken


#region Connect to Azure Automation Account

try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

#endregion 

#region Check Azure VMs in Each Resource Group

$ResourceGroups = Get-AzureRmResourceGroup 

foreach ($ResourceGroup in $ResourceGroups)
{
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroup.ResourceGroupName
    foreach($VM in $VMs)
    {
           $VMStatus = Get-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -Status
           If (($VMStatus.Statuses[1].DisplayStatus) -ne "VM deallocated") 
            {       

                [string]$message =  "$($vm.name)" + " status is " + "$($VMStatus.Statuses[1].DisplayStatus)"               
                Write-Output  -InputObject $message
                #Create Wunderlist task for inbox (your listid may be different!)
                New-WunderlistTask -listid 122588396 -title $($message)
            }  
    }
}
#endregion

We can now use our configured Source Control to synchronize the runbook to Azure Automation Account.

image

The Runbook is pushed to the remote Github Repository and will eventually show up in Azure Automation.

image

And after triggering a synchronization the new Runbook shows in the Azure Portal.

image

Let’s test the runbook.

image

Seems to be working ok.

image

And we can also check Wunderlist if this new task is being created.

image

Publish and Schedule Runbook

The final step is publishing and schedule the Runbook.

image

And now Schedule the Runbook

image

 

image

You can now enjoy your Wunderlist Runbook in Azure Automation. Let me know if you have some cook scenario’s to use this Wunderlist Module in Azure Automation.