Title: Copy Custom VM image from one resource group to other (in same subscription)
Tags: azure;azure-resource-manager;azure-sdk-.net
Question: I am trying to create a template deployment using Azure Resource Management library for .net from a custom VM image. The goal can be achieved by creating a resource group and deploy needed resources (using the template file) using the aforementioned library. There is a requirement that upon deleting the resource group i also need to delete the Vhd that is created upon VM creation in the resource group. but if i delete the resource group, vhd file is not deleted because it is created in different resource group (hence different storage account, where the vm image exists) and there is a fact that custom vm image need to be present at creation time in the very same storage account that will host your virtual machine's vhd. i cant delete the storage account containing the custom image. So, is there a way that i can copy the custom image (vhd) from a storage account to my newly created resource group's storage account using Resource Management library for .Net?
or are there any other workarounds to delete the vhd of created vm without deleting the custom vm image??


Here is another answer: There isn't any way to copy a blob during a template deployment, currently the storage resource provide doesn't support data plane operation during template deployments.  You could break it into multiple deployments, for example:
Deploy storage account for new VM into new resource group
Run code to copy custom image for VHD to new storage account in the new resource group
Deploy VM to new resource group
But - if you're doing this through code, likely the simplest approach is to grab the URI of the OS's VHD before you delete the Resource Group that holds the VM.  Then after you delete that RG, delete that blob.  This PowerShell code will give you the uri of the vhd blob for a VM (.net SDK will be similar).
(Get-AzureRmVm -ResourceGroupName [name of RG]).StorageProfile.OsDisk.Vhd.Uri
If you have more than one VM in the RG, then you'll get an array and you can iterate through it.
Comment for this answer: That's actually what I do for some of mine, there is a req that the storage account and VM be in the same location which may just mean keeping multiple copies of the base image.
Comment for this answer: Looks like there was a good response already - lmk if that doesn't work for some reason...
Comment for this answer: @bmoore-msft can you please give me some guidance on following question ? http://stackoverflow.com/questions/42933466/how-to-clone-and-manage-a-virtual-machine-in-azure-using-c-sharp-and-azure-net
Comment for this answer: Yes, makes sense. Another approach that i was thinking about could be to create a different container (in the storage account that holds vm's base image) and keep the vhd of created vm (from base image) into that container and deleting the container when required..

Here is another answer: Microsoft Azure Storage Explorer
Copy/Paste the VHD from
Storage Account A\Blob Containers\uploads
Storage Account B\Blob Containers\uploads

Here is another answer: Another possible workaround is to download the vhd file from the storage account to your local file system, delete the container or storage account, which ever you want. Then push this vhd file on the local system to the azure storage account you newly created using,
Add-azureRmVhd -Destination 'newstorageaccounturi' -LocalFilePath 'C:\users....\' -NumberOfUploaderThreads 5
Although, I do not understand why you want to delete the vhd of this created vm? Do you want to attach a different disk to it?
Comment for this answer: actually in my scenario vm is to be deleted after a specific amount of time and if its vhd doesn't get deleted then in long term it will increase storage cost as there are too many vms to be created and deleted.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:04/08/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<azure><azure-resource-manager><azure-sdk-.net>', 'Title': 'Copy Custom VM image from one resource group to other (in same subscription)', 'ViewCount': 2667, 'id': 36503300, 'user_AboutMe': '', 'user_AccountId': 3379326.0, 'user_DisplayName': 'Muhammad Murad Haider', 'user_Reputation': 1287.0, 'user_Views': 291.0, 'user_id': 2836750.0}}