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!
- All you need is Azure Automation Az-Module. We will utilize four functions from the module. Two primary operational functions (
Remove-AzObject) and two subsidiary functions (
Select-AzChildObject) for interactive object selection.
- 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 .
- The Azure allows to copy VHD of powered on VM! No need to deallocate VM and interrupt your workloads.
- The function has two mandatory parameters that initiated by subsidiary
-Blobparameter – this is the source VHD file and
-Container– the destination. There are two optional parameters:
-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 Containeroption in the
- The second stage is selecting
- Confirm your intention. Like every advanced function, the
-Verboseparameters as well.
- If copy was done, the function returns just copied VHD.
- If the destination contains VHD with the same name, you will get one more confirmation dialog. To suppress all confirmations, add
- First of all, why
Remove-AzBlob? The reason is
Remove-AzObjectcan 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
- The function returns Boolean value. If for some reason the object cannot be deleted you will get an error message.
-Filterparameter. 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
- With no
Select-AzChildObjectfunction 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 Contextchild object. With no
-CustomOutputthe function returns selected storage account access key object. The key itself saved in the
-CustomOutputreturns original storage context object.
Copy-AzBlobfunction works with custom object types, supplied with
Az-Module. It expects
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
Child Objectsmenu 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-AzChildObjectsupports both of them.
Select-AzObject StorageAccount | Select-AzChildObject Select-AzObject StorageAccount | Select-AzChildObject | Select-AzChildObject
- Here we are calling the
Select-AzChildObjecttwice, 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.
- 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
-Verboseparameter 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