次の方法で共有


Easily checking Azure Resource Manager deployment status from PowerShell

If you’re working with Azure Resource Manager templates on Windows then there’s a strong possibility that you’re using the Azure PowerShell cmdlets to deploy them. Note that if you’re on OSX/Linux you can use the Azure CLI. Oh, and it works on Windows as well! For an Azure CLI version of this post see https://blogs.msdn.com/b/stuartleeks/archive/2015/11/26/easily-checking-azure-resource-manager-deployment-status-from-bash-with-the-azure-cli.aspx

I’ve had a couple of mini-scripts that I’ve found myself using repeatedly, so I thought I’d tidy them up a bit and share them in case they’re useful for anyone else.

Note: the scripts for this use the 1.0 release of the Azure PowerShell cmdlets. See this link for information on the changes from previous versions: https://azure.microsoft.com/en-us/blog/azps-1-0/

Getting Set Up

Make sure you’ve got the Azure cmdlets installed and configured. To check, try running

Get-AzureRmResourceGroup

Grab the script from here: https://gist.github.com/stuartleeks/1dbe648bbfdc2a56f481, and save it as AzureHelpers.ps1. From a PowerShell prompt, dot source the script to include the cmdlets as shown below. Pay close attention to the dots :-)

. ./AzureHelpers.ps1

And that’s it, you’re all set!

Examples

Let’s take a look at a few examples to help get started with the utilities

Starting Simple

A simple example to get started with the cmdlets is:

Get-LastDeploymentOperation -ResourceGroupName "asetest"

The output from this will be something similar to:

Id
--
/subscriptions/e96f24a6-ceee-43a3-8ad4-5e5dca55656b/resourceGroups/asetest/providers/Microsoft.Resources/de...
/subscriptions/e96f24a6-ceee-43a3-8ad4-5e5dca55656b/resourceGroups/asetest/providers/Microsoft.Resources/de...
/subscriptions/e96f24a6-ceee-43a3-8ad4-5e5dca55656b/resourceGroups/asetest/providers/Microsoft.Resources/de...
/subscriptions/e96f24a6-ceee-43a3-8ad4-5e5dca55656b/resourceGroups/asetest/providers/Microsoft.Resources/de...

This is the set of outputs that the standard Azure cmdlet Get-AzureRmResourceGroupDeploymentOperation returned for the most recent deployment to the “asetest” resource group. However, this isn’t terribly easy to read!

Making It Readable

To address the readability challenge above, we can use another cmdlet from the helpers, ConvertTo-DeploymentOperationSummary, and pipe the results to that:

Get-LastDeploymentOperation -ResourceGroupName "asetest" `
| ConvertTo-DeploymentOperationSummary

The output from this looks like:

Id : BC1833006F6A1347
ProvisioningState : Failed
ResourceType : Microsoft.Web/sites/Extensions
ResourceName : WebApp-API-foo/MSDeploy
StartTime : 25/11/2015 12:28:46
EndTime : 25/11/2015 12:29:19
Duration : 00:00:33
Error : @{Code=ResourceDeploymentFailure; Message=The resource operation completed with terminal
provisioning state 'Failed'.}

Id : E4CE0CC89B2FA18F
ProvisioningState : Succeeded
ResourceType : Microsoft.Web/sites/config
ResourceName : WebApp-API-foo/web
StartTime : 25/11/2015 12:28:45
EndTime : 25/11/2015 12:28:48
Duration : 00:00:03
Error :

...

With this output format it is easy to format into even more of a summary for, work out which steps took the longest, or just filter to see which operations failed.

If you prefer to see the output in a more interactive way then you can output it to the GridView:

Get-LastDeploymentOperation -ResourceGroupName "asetest" `
    | ConvertTo-DeploymentOperationSummary `
| sort -Property StartTime `
| Out-GridView

This gives you output like the following where you can further filter and sort interactively

image

Going Further Back

The Get-LastDeploymentOperation cmdlet takes an additional parameter, DeploymentsToSkip. This specifies how many deployments to skip when before returning the operations for the deployment. It defaults to zero, i.e. just take the most recent deployment. The example below sets it to two, i.e. gets the operations for the deployment two before the most recent deployment:

Get-LastDeploymentOperation -ResourceGroupName "asetest" `
                            -DeploymentsToSkip 2

Wrapping Up

I’m trying to decide whether it would make sense to wrap this up as a module at some point (it would avoid the dot-sourcing step in favour of install-module), but other than that – enjoy!