Azure for Architects
上QQ阅读APP看书,第一时间看更新

Using Az modules

So far, all examples have used AzureRM modules. The previously shown runbooks will be re-written to use cmdlets from the Az module.

As mentioned before, Az modules are not installed by default. They can be installed using the Modules gallery menu item in Azure Automation.

Search for Az in the gallery and the results will show multiple modules related to it. If the Az module is selected to be imported and installed, it will throw an error saying that its dependent modules are not installed and that they should be installed before installing the current module. The module can be found on the Modules gallery blade by searching for Az, as shown in Figure 4.13:

Searching the Az module on the Modules gallery blade.
Figure 4.13: Finding the Az module on the Modules gallery blade

Instead of selecting the Az module, select Az.Accounts and import the module by following the wizard, as shown in Figure 4.14:

Importing the Az.Accounts module.
Figure 4.14: Importing the Az.Accounts module

After installing Az.Accounts, the Az.Resources module can be imported. Azure virtual machine-related cmdlets are available in the Az.Compute module, and it can also be imported using the same method as we used to import Az.Accounts.

Once these modules are imported, the runbooks can use the cmdlets provided by these modules. The previously shown ConnectAzure runbook has been modified to use the Az module:

param(

    [parameter(mandatory=$true)]

    [string] $connectionName

)

$connection = Get-AutomationConnection  -name $connectionName  

$subscriptionid = $connection.subscriptionid

$tenantid = $connection.tenantid

$applicationid = $connection.applicationid

$cretThumbprint = $connection.CertificateThumbprint

Login-AzAccount -CertificateThumbprint $cretThumbprint -ApplicationId $applicationid -ServicePrincipal -Tenant $tenantid  -SubscriptionId  $subscriptionid 

Get-AzVm

The last two lines of the code are important. They are using Az cmdlets instead of AzureRM cmdlets.

Executing this runbook will give results similar to this:

Output showing that the Az.Accounts module successfully imported and the status is displayed as completed.
Figure 4.15: The Az.Accounts module successfully imported

In the next section, we will work with webhooks.