Interactive Azure VHD operations w/PowerShell

Cover

In my previous article we talked about how to deploy Azure VM from existing VHD file/blob. Today we will automate all VHD related operations (Copy/Move/Rename/Delete) with PowerShell. And we will make this process fully interactive – menu driven with no mandatory parameters at all!

Az-Module

  • All you need is Azure Automation Az-Module. We will utilize four functions from the module. Two primary operational functions (Copy-AzBlob and Remove-AzObject) and two subsidiary functions (Select-AzObject and Select-AzChildObject) for interactive object selection.

Copy-AzBlob

  • This is the main function. It allows to make all possible copy variations: between two different Storage Accounts, between two containers of the same Storage Account and in place copy within the same container. Of course if you have enough rights to make all these operations Emoj.

  • The Azure allows to copy VHD of powered on VM! No need to deallocate VM and interrupt your workloads.

01.Copy-AzBlob_Get-AzVm

  • The function has two mandatory parameters that initiated by subsidiary Select- functions. The -Blob parameter – this is the source VHD file and -Container – the destination. There are two optional parameters: -NewName and -Move, their names are self-explanatory.
Select-AzObject SA | Select-AzChildObject -CustomOutput -Verbose | Copy-AzBlob -Container (Select-AzObject SA | Select-AzChildObject -CustomOutput -Verbose) -NewName azvm1_osdisk_copy.vhd
  • Let’s see in a detail this one-liner. Very first stage is to select destination container (the Blob Container option in the Child Objects menu).

02.Copy-AzBlob_Select-DestContainer

  • The second stage is selecting Blob in the Child Objects menu.

03.Copy-AzBlob_Select-SourceBlob1

  • Confirm your intention. Like every advanced function, the Copy-AzBlob supports -Confirm and -Verbose parameters as well.

04.Copy-AzBlob_Select-SourceBlob2

  • If copy was done, the function returns just copied VHD.

05.Copy-AzBlob_Select-SourceBlob3

  • If the destination contains VHD with the same name, you will get one more confirmation dialog. To suppress all confirmations, add -Confirm:$false parameter.

06.Copy-AzBlob_Confirm-Overwrire

Remove-AzObject

  • First of all, why Remove-AzObject and not Remove-AzBlob? The reason is Remove-AzObject can deal with many other object types in addition to the blobs. In the current version it supports blobs, containers and virtual network adapters deletion. Just pass supported object to the function and it will be deleted, very easy.
Select-AzObject StorageAccount | Select-AzChildObject | Remove-AzObject –Verbose
Select-AzObject VM | Select-AzChildObject | Remove-AzObject

07.Remove-AzObject_Success

  • The function returns Boolean value. If for some reason the object cannot be deleted you will get an error message.

08.Remove-AzObject_Fail

Select-AzObject/Select-AzChildObject

  • Both Select- functions support -Filter parameter. It intended to truncate long menu lists. The parameter is positional and supports wildcards.
Select-AzObject VM -Filter dev* | Get-AzVmPowerState
Select-AzObject VNET *test* | Get-AzSubnet
Select-AzObject SA | Select-AzChildObject –Filter *.vhd
Select-AzObject SA | Select-AzChildObject boot*.bmp

Select-AzChildObject

  • With no -CustomOutput parameter the Select-AzChildObject function returns original Microsoft’s objects.
Select-AzObject StorageAccount | Select-AzChildObject | Get-Member
Select-AzObject VM | Select-AzChildObject | Get-Member
  • The only exception from this rule is Storage Context child object. With no -CustomOutput the function returns selected storage account access key object. The key itself saved in the Value property.

09.Select-AzChildObject_StorageContextKey

  • The -CustomOutput returns original storage context object.

10.Select-AzChildObject_StorageContextCustomObject

  • The Copy-AzBlob function works with custom object types, supplied with Az-Module. It expects [AzBlob] object in -Blob parameter and [AzBlobContainer] in the -Container.
Select-AzObject StorageAccount | Select-AzChildObject -CustomOutput | Get-Member
  • The custom objects unlike their Microsoft’s parent objects are not embedded and more readable. Moreover, they have some usable methods. Select Blob Container or Blob in the Child Objects menu and investigate returned object.
$customAzObject = Select-AzObject StorageAccount | Select-AzChildObject -CustomOutput
$customAzObject | Get-Member -MemberType Method
$customAzObject.GetStorageAccount()
$customAzObject.GetParent()
$customAzObject.ToString()
  • When you want to select a blob, the parent object may be either Storage Account or Container, the Select-AzChildObject supports both of them.
Select-AzObject StorageAccount | Select-AzChildObject
Select-AzObject StorageAccount | Select-AzChildObject | Select-AzChildObject
  • Here we are calling the Select-AzChildObject twice, the first call to select a Container and the second time to select its child Blob. This method can be useful to improve performance in “populous” Storage Accounts. The blobs retrieved in bulks of ten in parallel.

Summary

  • For more details about the functions, please take a look at the content based help and examples.
Get-Alias –Module Az-Module
Get-Help Remove-AzObject
Get-Help Copy-AzBlob -Full
Get-Help Select-AzChildObject -Examples
Get-Help Select-AzObject -Parameter Filter
  • All above functions are advanced and support -Verbose parameter for more verbose output.

You might also like

Azure Automation – Increase Azure VM disks
Get-AzSubnet – PowerShell based IPAM for the Azure IaaS cloud
New-AzParamsJson – Create Azure JSON parameter files
Deploy-AzureVmVhd.ps1 – Deploy Azure VM from VHD

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s