Against the backdrop of a sparkling lake, an outstretched palm holds a blue, virtual cube.

Azure – Move Your Resources and Save Cash

Have you ever found that Azure VM that was causing a financial black hole? Maybe the one created with the wrong size, in the wrong location, and with the wrong storage type? Go no further.

Issue

Recently we ran into a customer that, when going through an Azure workshop, created multiple VMs with one or more of these financial pitfalls across multiple Azure VMs. The folks asked if there was an easy way to simply move the storage type and VM location. Right away our answer was – Sure! We have moved things in the past. But when we started to break it down, you have to move multiple pieces and it can take a bit of time and code to get the job done. So we created an Azure PowerShell script offline that helped move these resources to the correct type and place.

Resolution

We wrote a bit of PowerShell code that would clone a VM. This included moving to a different data center, associated Storage (Storage Accounts, Containers, and Disks), replicate Networking (Network Security Groups, Subnet, Network Interface, and Public & Private IPs), and resize the VM all at the same time.

Code Review

The PowerShell code can be found here:  Download File

Parameters
  • Parameter [string] = $sourceVmName = Source VM name
  • Parameter [string] = $destinationVmName = Destination VM name (and the destination RG)
  • Parameter [string] = $destinationResourceGroup = Name the RG different then the destination VM name
  • Parameter [string] = $subscriptionID = Subscription ID
  • Parameter [string] = $destinationResourceGroupLocation = Destination Location IE eastus,centralus
  • Parameter [string] = $destinationStorageType = Destination storage type IE Premium_LRS, Standard_LRS
  • Parameter [switch] = $powerDownSource = Power down the source VM
  • Parameter [switch] = $newVmSize = Pick another size then the source VM
  • Parameter [switch] = $skipAuth = Skips logging into Azure because you already are logged in
Example
 .\cloneVM.ps1 -sourceVmName desktop-123 -destinationVmName waka-44rfv 

Output

 .\cloneVM.ps1 -sourceVmName desktop-123 -destinationVmName wakapp-iu -newVmSize

Environment : AzureCloud
Account : spr@properevents.org
TenantId :
SubscriptionId :
SubscriptionName : Free Trial
CurrentStorageAccount :

Resource group 'wakabpp-iu' does not exist. To create a new resource group, please enter a location.
Creating resource group 'wakabpp-iu' in location 'eastus'

ResourceGroupName : wakabpp-iu
Location : eastus
ProvisioningState : Succeeded
Tags :
TagsTable :
ResourceId : /subscriptions//resourceGroups/wakabpp-iu

ResourceGroupName : wakabpp-iu
StorageAccountName : desktop123disks211375
Id : /subscriptions//resourceGroups/wakabpp-iu/providers/Microsoft.Storage/storageAccounts/desktop123disks211375
Location : eastus
Sku : Microsoft.Azure.Management.Storage.Models.Sku
Kind : Storage
Encryption :
AccessTier :
CreationTime : 2/22/2017 5:16:23 PM
CustomDomain :
LastGeoFailoverTime :
PrimaryEndpoints : Microsoft.Azure.Management.Storage.Models.Endpoints
PrimaryLocation : eastus
ProvisioningState : Succeeded
SecondaryEndpoints :
SecondaryLocation :
StatusOfPrimary : Available
StatusOfSecondary :
Tags : {}
Context : Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext

ResourceGroupName : wakabpp-iu
StorageAccountName : test4433e375
Id : /subscriptions//resourceGroups/wakabpp-iu/providers/Microsoft.Storage/storageAccounts/test4433e375
Location : eastus
Sku : Microsoft.Azure.Management.Storage.Models.Sku
Kind : Storage
Encryption :
AccessTier :
CreationTime : 2/22/2017 5:17:08 PM
CustomDomain :
LastGeoFailoverTime :
PrimaryEndpoints : Microsoft.Azure.Management.Storage.Models.Endpoints
PrimaryLocation : eastus
ProvisioningState : Succeeded
SecondaryEndpoints :
SecondaryLocation :
StatusOfPrimary : Available
StatusOfSecondary :
Tags : {}
Context : Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext

CloudBlobContainer : Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
Permission : Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
PublicAccess : Off
LastModified : 2/22/2017 5:17:40 PM +00:00
ContinuationToken :
Context : Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext
Name : vhds

CloudBlobContainer : Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
Permission : Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
PublicAccess : Off
LastModified : 2/22/2017 5:17:42 PM +00:00
ContinuationToken :
Context : Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext
Name : vhd

Please wait for drives to finish copying.

CopyId : 94796766-4e18-437e-beba-4ada6aaf5eb0
CompletionTime : 2/22/2017 5:18:12 PM +00:00
Status : Success
Source : https://desktop123disks211.blob.core.windows.net/vhds/Desktop-123-20170209-103611.vhd?sv=2015-04-05&sr=b&sig=9ZbVdD3epq58bfvl%2BnR8BURUF4g6LpJJ2kBJGDdGtrM%3D&se=2017-03-01T17:17:4
1Z&sp=r
BytesCopied : 1098437886464
TotalBytes : 1098437886464
StatusDescription :

Please wait for drives to finish copying.

CopyId : a0ce5256-027c-4c89-ac15-b92c3561ef49
CompletionTime : 2/22/2017 5:18:12 PM +00:00
Status : Success
Source : https://desktop123disks211.blob.core.windows.net/vhds/Desktop-123-20170209-104305.vhd?sv=2015-04-05&sr=b&sig=gypjy091%2FAI9vC63%2BBUedb3tAZILu57QoCYtdaHVAm0%3D&se=2017-03-01T17:17
:42Z&sp=r
BytesCopied : 1098437886464
TotalBytes : 1098437886464
StatusDescription :

Please wait for drives to finish copying.

CopyId : be6d3773-a81c-48dc-ab6f-41cf57fd298d
CompletionTime : 2/22/2017 5:18:20 PM +00:00
Status : Success
Source : https://test4433e.blob.core.windows.net/vhd/Desktop-123-20170221-090029.vhd?sv=2015-04-05&sr=b&sig=KwyAUt%2FwEGQuzbc2DevCEHOU6rRP6q1%2FCEKO6FVV0m4%3D&se=2017-03-01T17:17:43Z&sp=r
BytesCopied : 1098437886464
TotalBytes : 1098437886464
StatusDescription :

Please wait for drives to finish copying.

CopyId : 236bfe9b-4f6a-4480-b05d-4234dbe62de7
CompletionTime : 2/22/2017 5:27:00 PM +00:00
Status : Success
Source : https://desktop123disks211.blob.core.windows.net/vhds/Desktop-12320170131104057.vhd?sv=2015-04-05&sr=b&sig=QNuykal3bAmr5vIWkkrWdmNNPSI%2Br4vzLhd4sBjOZxM%3D&se=2017-03-01T17:17:44Z
&sp=r
BytesCopied : 136365212160
TotalBytes : 136365212160
StatusDescription :

RequestId :
IsSuccessStatusCode : True
StatusCode : OK
ReasonPhrase : OK 

Tags> , , , ,

William Lee

William Lee

William Lee is a Senior Consultant in the Cloud Practice at SPR Consulting.