Publish Custom PowerShell Workflows to Azure Automation

Introduction

Writing Runbooks in Az Automation is possible in the following languages: PowerShell and Python, in PowerShell it is also possible to write PowerShell Workflow.

In this blog post, I will walk through some highlights in writing ‘PowerShell Workflow’ and how to upload it to ‘Runbook gallery’ in Azure Automation.

The pros and cons of using Workflow

The benefits used in Powershell Workflow is especially in long processes, and those that perform several operations in parallel, to the long ones we add Checkpoint [Checkpoint-Workflow function] so that at the moment of a fall or when the process runs over 3 hours, the process will run again from the last point – ‘Checkpoint‘,

The advantage is also the ease of realization of the definition, for processes for example that work in parallel, we will only need to add Parallel function and the same goes for checkpoints:

Parallel

{

    <Activity1>

    <Activity2>

}

Parallel function

The downside of Workflow is, that there are a lot of PowerShell Native operations, that need to be added to InlineScript which limits and complicates the work.
Every action performed within the process is called Activity, the FW (Workflow) automatically converts any PowerShell command to Activity.
If there is no command in the process that matches Activity, the command ran in Inline Script Activity.
The main disadvantage of Inline Activity:

  1. Positional parameters cannot be used (implementation of the $ _ in the script), and FilterScript must be added after the Where object as follows:
    Workflow Get-RunningServices
    {
    Get-Service | Where-Object -FilterScript {$ _. Status -eq “Running”}
    }

2. Variables in WF retain the Properties but not the Method, which does not allow entering the variable and performing operations unless the entire command is in Inline script.

Any PowerShell Script can be turned into a Workflow All it takes is:

Give the same name to the Workflow and the Script name.

Add the functions in Workflow.

Publishing Workflow

For the purpose of the example, I took two scripts from Microsoft docs, that perform Unattached Disks removal.

Script that removes Unleased Managed Disks and VHD files (Unmanaged Disks), I set them up in PowerShell Workflow and added a Checkpoint between them.

The script – https://www.powershellgallery.com/packages/Remove-UnattachedDisks/1.0

First script A script runs as-is, in the second script because of using PowerShell Native, I had to put it in ‘Inline Script’.

So now that we have a written workflow, you can share it with the community…

In order for us to see a Runbook in ‘Runbook Gallery’ we first need to upload it to ‘PowerShell Gallery’ and that is why we need to register first.

  • Create a new Key, and give a name, copy the key which we’ll use to upload the script
The Glob pattern can be *
  • We will now update the script we prepared earlier (the Custom workflow) in additional fields so we can upload it:
    • Update-ScriptFileInfo -Path ‘C:\<Script path>\<WF Script>.ps1’ -tags “Windows Azure” -Version 1.0 -Author “<Your Name>” -RequiredModules <for example = Az.Compute,Az.Accounts..> -Description “Remove Unattached Disks from Azure subscription” -Force
  • Publish script:
    • Publish-Script -Path ‘C:\<Script path>\<WF Script>.ps1‘ -Repository PSGallery -NuGetApiKey <API Key> -Force
  • Make sure the script has uploaded successfully
    • Find-Script -Repository PSGallery -Name “<Script Name>”
    • after few minutes, we can also see on the site
  • Now move on to Azure Automation, go to Runbook Gallery, change the source to “PowerShell Gallery” and search the script

BTW the script can be deployed from Powershell gallery directly to your subscription by clicking on “Deploy to Azure Automation” tab

Authors