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.
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:
- 18 Steps for End-to-End IaaS Provisioning in the Cloud with Azure Resource Manager (ARM), PowerShell and Desired State Configuration (DSC)
- HOW-TO: Building VNET-to-VNET Connections with Azure Resource Manager (ARM) and PowerShell in 5 Steps
- Export Azure Subscription Usage to CSV with NEW Billing API and PowerShell
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
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
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
# Get VM OS Profile
$vm.OsProfile
# Get VM Hardware Profile
$vm.HardwareProfile
# Get Storage Profile
$vmStorage = $vm.StorageProfile
$vmStorage.ImageReference
$vmStorage.OsDisk
# 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}}
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