Step-by-Step: Managing Azure Resources with Azure Resource Manager (ARM) and PowerShell

In the past few articles, we’ve been focusing on provisioning end-to-end IaaS environments on the Microsoft Azure cloud platform using the new Azure Resource Manager API and PowerShell.  In this article, we’ll be looking at several ways to manage these environments post-provisioning using some of the new capabilities, such as Resource Groups and Tags, that ARM provides.

Managing Azure Resources with Azure Resource Manager and PowerShell

Before you Begin …

If you haven’t been following along with prior articles, here’s a few articles that you should review before continuing on with this article, because they provide a foundation on which this article is based:

At a minimum, you’ll need to complete the first article above before proceeding with this article.  When you’ve finished that, come back here to pick up with managing the resources you’ve provisioned.

Get all resources in a Resource Group

In ARM, Resource Groups give us the ability to logically group related resources for monitoring, administration tasks and usage tracking.  Let’s first generate a list of all resources within a particular resource group:

# Switch to Azure Resource Manager mode

Switch-Mode `
-Name AzureResourceManager

# Select an Azure Resource Group

$rgName = `
(Get-AzureResourceGroup).ResourceGroupName |
Out-GridView `
-Title "Select a Resource Group" `
-PassThru

# Get all Azure Resources within Resource Group

Get-AzureResource `
-ResourceGroupName $rgName `
-OutputObjectFormat New |
Select-Object `
Name,
ResourceType,
Location

Click to zoom ...

In the output above, you may notice that Resource Groups can group together:

  • Different types of resources (ie., Network, Compute and Storage)
  • Resources located in different Azure datacenter regions

This provides a great deal of flexibility when using Resource Groups as logical “containers” for managing complex global application workloads.

Get Azure Resources by Tags

ARM also provides the ability to tag resources with one or more custom tag values.  This can be useful to tag certain resources related to a common project, department or cost center, even if those resources don’t exist in the same Resource Group.

# Get Azure Resources by Tag

Get-AzureResource `
-TagName "project" `
-TagValue "demo" `
-OutputObjectFormat New |
Select-Object `
Name,
ResourceGroupName,
ResourceType,
Location

Click to zoom ...

In this example, we returned all resources tagged with a common project value, regardless of the Resource Group in which those resources were originally provisioned.

Get all properties of resources in a Resource Group

# Get properties of all VMs in a Resource Group

$vm = (Get-AzureResource `
-ResourceGroupName $rgName `
-ResourceType "Microsoft.Compute/virtualMachines" `
-OutputObjectFormat New `
-ExpandProperties).Properties

$vm

Click to zoom ...

# Get VM OS Profile

$vm.OsProfile

Click to zoom ...

# Get VM Hardware Profile

$vm.HardwareProfile

Click to zoom ...

# Get Storage Profile

$vmStorage = $vm.StorageProfile

$vmStorage.ImageReference

Click to zoom ...

$vmStorage.OsDisk

Click to zoom ...

# Get VM Network Info

$vmNetwork = $vm.NetworkProfile

$vmNetwork.NetworkInterfaces.Id |
ForEach-Object {
$_.Split("/")[-1]
} |
ForEach-Object {
Get-AzureNetworkInterface `
-Name $_ `
-ResourceGroupName $rgName
}

Check Status of all VMs in a Resource Group

# Check the status of all VMs in a Resource Group

Get-AzureResource `
-ResourceGroupName $rgName `
-ResourceType "Microsoft.Compute/virtualMachines" `
-OutputObjectFormat New |
Get-AzureVM `
-Status |
Select-Object `
Name,
@{n="Status";e={$_.Statuses[-1].DisplayStatus}}

Click to zoom ...

Stop & Start all VMs in a Resource Group

# Stop all VMs within a Resource Group

Get-AzureResource `
-ResourceGroupName $rgName `
-ResourceType "Microsoft.Compute/virtualMachines" `
-OutputObjectFormat New |
Stop-AzureVM –Force

# Start all VMs within a Resource Group

Get-AzureResource `
-ResourceGroupName $rgName `
-ResourceType "Microsoft.Compute/virtualMachines" `
-OutputObjectFormat New |
Start-AzureVM

To be continued …

I hope this article was helpful in getting you started with basic management of Azure resources via the new Azure Resource Manager API and PowerShell.  In future articles, we’ll be covering additional management topics, so be sure to let us know which tasks you are most interested in learning more about.

Until then … See you in the Clouds!

- Keith