Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

How to manage your cloud environments after provisioning.

· Cloud Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

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

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!

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:
cloud ,azure

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}