How to use Az module in Azure automation runbooks?
Azure Automation delivers a cloud-based automation that supports consistent management in your Azure cloud and hybrid environments. It comprises of process automation, configuration management, update management, shared capabilities, and heterogeneous features. Automation gives you complete control during deployment, operations, and decommissioning of workloads and resources.
To achieve process automation in Azure you have to create runbooks in Powershell (graphical, script, workflow) or Python2. For details, see Azure Automation runbooks.
Azure provides 2 types of Resource Modules. AzureRm and Az
Why a new module?
Az offers shorter commands, improved stability, and cross-platform support. Az also has feature parity with AzureRM, giving you an easy migration path. With the Az module, Azure PowerShell is now compatible with PowerShell 5.1 on Windows and PowerShell Core 6.x and later on all supported platforms — including Windows, macOS, and Linux.
Unfortunately Azure Automation is shipped with AzureRm modules and it’s going to be a re-work to port your work to AzureRm modules. The good news is, you can easily import the new “Az” modules. Once you provisioned the Azure automation account, head over to Shared Resources >> Modules section.
Click on Browse GalleryType Az.Compute or Az.StorageClick on Import
It will take few minutes to import the module in to your automation account. After that you are free to use the imported Az module(s). You may have to manually download the Az modules like Az.Storage etc..
Assuming you have some powershell code written using Az modules, you can just create a powershell based runbook, paste the code here, test it and publish the runbook. You can further automate it based on your business needs.
Here’s an example of Powershell based Automation Runbook using Az module
$connection = Get-AutomationConnection -Name AzureRunAsConnection$connectionResult = Connect-AzAccount `-ServicePrincipal `-Tenant $connection.TenantID `-ApplicationId $connection.ApplicationID `-CertificateThumbprint $connection.CertificateThumbprint"Login successful.."New-AzResourceGroup -Name TutorialResources -Location eastus
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."$vmParams = @{
ResourceGroupName = 'myRG'
Name = 'testVM1'
Location = 'eastus'
ImageName = 'Win2016Datacenter'
PublicIpAddressName = 'testvm1pip'
Credential = $cred
OpenPorts = 3389
}
$newVM1 = New-AzVM @vmParams
Once the VM is ready, we can view the results in the Azure Portal or by inspecting the $newVM1 variable.
$newVM1
References:
https://docs.microsoft.com/en-us/azure/automation/shared-resources/modules#migrating-to-az-modules