Developing Azure Automation Runbooks–using correct modules
When developing Runbooks for Azure Automation I noticed that it’s important to use the same (Global) PowerShell Module versions as available as Asset in Azure Automation. But let’s first talk about Azure Automation Assets a bit more.
Automation Assets
Automation Assets are various resources that are globally available to be used in or associated with a Runbook. Assets include Schedules, Modules, Certificates, Connections, Variables and Credentials.
For more information about the Automation Assets check the following Automation Documentation page: https://azure.microsoft.com/en-us/documentation/services/automation/
Modules
If look at the Modules Asset we see some default available Global modules and some Integration Modules. The Global Modules have been updated automatically by the Automation Service.
You cannot remove Global Modules.
If we retrieve some more details from one of the Global Modules (example. AzureRM.Storage) we get the following info.
So why would you care about this info?
If you are like me, you always want to use the latest and greatest tools as soon as they are available. This is also true for my PowerShell Modules. I regularly check the PowerShell Gallery for the latest available PowerShell Modules.
Let’s check what the latest available version of the AzureRM.Storage Module is on the PowerShell Gallery.
Code Snippet
- Find-Module -name AzureRM.Storage
So what does this tell us? On the PowerShell Gallery there is a newer version of the AzureRM.Storage Module available. If you use this latest version in one of the Azure Automation Runbooks you are developing it could fail when using the Global AzureRM.Storage module on Azure Automation.
That’s why I was looking for a way to retrieve the Global Module versions being used in Azure Automation and download those modules from the PowerShell Gallery when developing and testing my Runbooks locally.
Let see the script in Action first.
Here is the script I used to download the same Global Modules used in Azure Automation locally. After Saving you still need to install these modules in your PSModulePath Environment variable folders.
Code Snippet
#requires -Version 3 -Modules PowerShellGet
# ---------------------------------------------------
# Script: C:\Users\stefstr\OneDrive - Microsoft\Scripts\PS\Azure\Automation\ShowAzureAutomationModulesInfo.ps1
# Version: 0.1
# Author: Stefan Stranger
# Date: 06/15/2016 09:15:31
# Description: Show Azure Automation Module Info and Download the same Modules as used in Azure Automation
# 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!
# ---------------------------------------------------
#Login to Azure
Add-AzureRmAccount
#Select Azure Subscription
$subscription =
(Get-AzureRmSubscription |
Out-GridView `
-Title 'Select an Azure Subscription ...' `
-PassThru)
Set-AzureRmContext -SubscriptionId $subscription.subscriptionId -TenantId $subscription.TenantID
#Select ResourceGroup
$ResourceGroup = Get-AzureRmResourceGroup | Out-GridView -PassThru
#Select AutomationAccount
$AutomationAccountName = Get-AzureRmAutomationAccount -ResourceGroupName $ResourceGroup.ResourceGroupName | Out-GridView -PassThru
#Retrieve all used Azure Modules within Azure Automation
$ModulesOutput = Get-AzureRmAutomationModule -ResourceGroupName $ResourceGroup.ResourceGroupName -AutomationAccountName $AutomationAccountName.AutomationAccountName | Where-Object -FilterScript {
$_.Name -like '*Azure*'
}
$endresult = @()
foreach ($Module in $ModulesOutput)
{
$Module = Get-AzureRmAutomationModule -Name $Module.Name -ResourceGroupName $ResourceGroup.ResourceGroupName -AutomationAccountName $AutomationAccountName.AutomationAccountName
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ModuleName -Value $($Module.name)
$obj | Add-Member -MemberType NoteProperty -Name IsGlobal -Value $($Module.IsGlobal)
$obj | Add-Member -MemberType NoteProperty -Name Version -Value $($Module.version)
$obj | Add-Member -MemberType NoteProperty -Name SizeInBytes -Value $($Module.SizeInBytes)
$obj | Add-Member -MemberType NoteProperty -Name CreationTime -Value $($Module.CreationTime)
$obj | Add-Member -MemberType NoteProperty -Name LastModifiedTime -Value $($Module.LastModifiedTime)
$obj = $obj | Add-Member -MemberType NoteProperty -Name ProvisioningState -Value $($Module.ProvisioningState) -PassThru
$endresult = $endresult + $obj
}
$endresult
#Check if modules can downloaded from PSGallery
$psgallery = @()
foreach ($azuremodule in $endresult)
{
$psgallerymodule = Find-Module -Name $azuremodule.ModuleName -MaximumVersion $azuremodule.version -ErrorAction SilentlyContinue
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name Version -Value $($psgallerymodule.Version)
$obj | Add-Member -MemberType NoteProperty -Name Name -Value $($psgallerymodule.Name)
$obj | Add-Member -MemberType NoteProperty -Name Type -Value $($psgallerymodule.Type)
$obj | Add-Member -MemberType NoteProperty -Name Repository -Value $($psgallerymodule.Repository)
$obj = $obj | Add-Member -MemberType NoteProperty -Name Description -Value $($psgallerymodule.Description) -PassThru
$psgallery = $psgallery + $obj
}
#Select Modules you want to download from PSGallery
$psgallery |
Out-GridView -Title 'Select Module to save' -OutputMode Multiple |
ForEach-Object -Process {
Save-Module -Name $($_.name) -MaximumVersion $($_.version) -Path $env:TEMP
}
Hope this helps.
Comments
- Anonymous
January 29, 2017
nice information