how to copy vhd to RM based subscription using powershell? - azure

I am looking for Powershell script to copy vhd to subscription using Resource Manager deployment model.

vhd's are stored in Azure storage page blobs, and the storage API works independently of how an account is created (classic vs ARM). It's even independent of subscription. You just need your storage account's name and key.
So you should just be able to copy via something like:
$DestContext = New-AzureStorageContext -StorageAccountName "<name>"
-StorageAccountKey "<key>"
Start-AzureStorageBlobCopy -DestContainer "<containername>" -DestContext $DestContext
And Start-AzureStorageBlobCopy should work the same, regardless how the storage account was created.

Related

Copy VHD from azure disk to azure storage account

I have an Azure disk of Windows 10 system. I want this disk must be converted into VHD and should be available in the azure storage account.
In easier words, I want my Azure VM data disk to be present in the azure storage BLOB without downloading the VHD onto my base machine. Is it possible. If yes, please guide me.
Can anyone please help me in this.
Thanks
Yes, it's possible. You can copy the managed disk into the VHD file and store it in the storage. Here is the example code through PowerShell:
#Provide the subscription Id of the subscription where managed disk is created
$subscriptionId = "yourSubscriptionId"
#Provide the name of your resource group where managed is created
$resourceGroupName ="yourResourceGroupName"
#Provide the managed disk name
$diskName = "yourDiskName"
#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://learn.microsoft.com/en-us/Az.Storage/storage-dotnet-shared-access-signature-part-1
$sasExpiryDuration = "3600"
#Provide storage account name where you want to copy the underlying VHD of the managed disk.
$storageAccountName = "yourstorageaccountName"
#Name of the storage container where the downloaded VHD will be stored
$storageContainerName = "yourstoragecontainername"
#Provide the key of the storage account where you want to copy the VHD of the managed disk.
$storageAccountKey = 'yourStorageAccountKey'
#Provide the name of the destination VHD file to which the VHD of the managed disk will be copied.
$destinationVHDFileName = "yourvhdfilename"
#Set the value to 1 to use AzCopy tool to download the data. This is the recommended option for faster copy.
#Download AzCopy v10 from the link here: https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10
#Ensure that AzCopy is downloaded in the same folder as this file
#If you set the value to 0 then Start-AzStorageBlobCopy will be used. Azure storage will asynchronously copy the data.
$useAzCopy = 1
# Set the context to the subscription Id where managed disk is created
Select-AzSubscription -SubscriptionId $SubscriptionId
#Generate the SAS for the managed disk
$sas = Grant-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $diskName -DurationInSecond $sasExpiryDuration -Access Read
#Create the context of the storage account where the underlying VHD of the managed disk will be copied
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
#Copy the VHD of the managed disk to the storage account
if($useAzCopy -eq 1)
{
$containerSASURI = New-AzStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds($sasExpiryDuration) -FullUri -Name $storageContainerName -Permission rw
$containername,$sastokenkey = $containerSASURI -split "\?"
$containerSASURI = "$containername/$destinationVHDFileName`?$sastokenkey"
azcopy copy $sas.AccessSAS $containerSASURI
}else{
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName
}
But in my test, there is something wrong with the VHD file when you use the azcopy command. And the command Start-AzStorageBlobCopy works well. So I recommend command Start-AzStorageBlobCopy for you. And for more details, see Export/Copy the VHD of a managed disk to a storage account in different region with PowerShell.

Azure managed disk from source blob (vhd) is not modified (updated)

I am a bit confused with the Azure managed data disk (in ARM) created from VHDs stored in storage source blob. I have a script that creates snapshots on all the storage blobs, but for some reason the changes that are made on the managed disks are not applied to the VHDs.
So the previous dependencies were like:
storage blob -> container -> VHDs -> VMs
Currently:
storage blob -> container -> VHDs -> managed data disks -> VMs
I also have made sure when the VMs were created to use additional data disks, which were created from the existing VHDs. Any ideas about the synchronization?
but for some reason the changes that are made on the managed disks
are not applied to the VHDs.
If you create snapshot of managed disk, you can find snapshot via Azure portal, like this:
But if you create snapshot of VHDs(unmanaged disk), the snapshot will not show in Azure portal, you should use Azure PowerShell to list it:
$rg = "Your_resource_group_name"
$saname = "Your_storage_account_name"
$con = "vhds"
$keylist = Get-AzureRmStorageAccountKey -ResourceGroupName $RG -StorageAccountName $saname
$Key = $Keylist[0].Value
$Ctx = New-AzureStorageContext -StorageAccountName $SAname -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob *.vhd -Container $Con -Context $Ctx
Output like this:
When you create a managed disk from your own VHD blob your blob is copied to create the managed disk's blob in a different account. The managed disk does not even exist in your account, so taking snapshots of the original blob won't show any updates. You could even delete the original VHD blob and the managed disk will still exist.
If you want to create another VM with a copy of the modified managed disk you need to create a managed snapshot of that disk and use that managed snapshot as a source for your next VM.

Is there a way to view blobs of managed disks in Microsoft Azure Storage Explorer or the Azure Portal?

When I create a VM using Azure Resource Manager with an unmanaged disk, I can view its .vhd in Microsft Azure Storage Explorer and/or the Azure Portal under the specified storage account's Blob Container in a sub-container called "vhds".
When I create a VM using Azure Resource Manager with a managed disk, I can't find any storage container with anything related to this managed disk.
I understand that Azure is "managing" this storage for me, per this Microsoft doc, and also that I can generate a "SAS Url" for this disk (which gives me no real info on where the blob is stored), but I believe (let me know if I'm wrong), that Azure must be storing my "managed disk" in a storage account within my subscription, and that I should be able to see the blobs for these "managed disks" somewhere in Microsoft Azure Storage Explorer or in the Azure Portal.
Is there an easy way to view these managed disk .vhds in either the portal or Microsoft Azure Storage Explorer?
For now, it is not possible to view managed disk .vhds on Portal or Microsoft Azure Storage Explorer.
Managed disk is different from unmanaged disk, managed disks are stored in a Microsoft managed storage account. For now, we could not view it. You could copy managed disks to your private storage account.
##create $SAS
$sas = Grant-AzureRmDiskAccess -ResourceGroupName shui -DiskName shuitest -DurationInSecond 3600 -Access Read
$destContext = New-AzureStorageContext –StorageAccountName contosostorageav1 -StorageAccountKey 'YourStorageAccountKey'
Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer 'vhds' -DestContext $destContext -DestBlob 'MyDestinationBlobName.vhd'
Notes:
If the managed disk attach to a VM, you should stop your VM, then you could copy it to your storage account.

Azure Managed Disks. Access to underlying blob?

Would it be possible to access the blob in Azure managed disks? If say ,I needed to copy it to another storage account(regular storage account). Since managed storage only support LRS at the moment.
If say ,I needed to copy it to another storage account(regular storage
account).
You should understand the difference between managed disks and
unmanaged disks. With unmanaged disks, you had to create storage
accounts to hold the disks (VHD files) for your Azure VMs. When
scaling up, you had to make sure you created additional storage
accounts so you didn’t exceed the IOPS limit for storage with any of
your disks. With Managed Disks handling storage, you are no longer
limited by the storage account limits (such as 20,000 IOPS / account).
You also no longer have to copy your custom images (VHD files) to
multiple storage accounts. You can manage them in a central location –
one storage account per Azure region – and use them to create hundreds
of VMs in a subscription. More information please refer to this
link.
Update:
You could copy managed disk to your private storage account by using the following cmdlets.
$sas = Grant-AzureRmDiskAccess -ResourceGroupName shui -DiskName shuitest -DurationInSecond 3600 -Access Read
$destContext = New-AzureStorageContext –StorageAccountName contosostorageav1 -StorageAccountKey 'YourStorageAccountKey'
Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer 'vhds' -DestContext $destContext -DestBlob 'MyDestinationBlobName.vhd'
You can't copy to a regular storage account, but you can create copies of it to any location you want. Let's suppose the disk is in "eastus" and you want a copy in "brazilsouth"
Get disk:
$disk = Get-AzureRmDisk -ResourceGroupName $rgName -DiskName $diskName
Make a copy config to another location:
$location = "brazilsouth"
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $location
Create a snapshot:
New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $newDiskName -ResourceGroupName $rgName
All done! This way you can keep a secondary copy in another datacenter location.

Is it possible to copy Azure CloudBlockBlobs between different Azure portal directories? [duplicate]

I am looking for Powershell script to copy vhd to subscription using Resource Manager deployment model.
vhd's are stored in Azure storage page blobs, and the storage API works independently of how an account is created (classic vs ARM). It's even independent of subscription. You just need your storage account's name and key.
So you should just be able to copy via something like:
$DestContext = New-AzureStorageContext -StorageAccountName "<name>"
-StorageAccountKey "<key>"
Start-AzureStorageBlobCopy -DestContainer "<containername>" -DestContext $DestContext
And Start-AzureStorageBlobCopy should work the same, regardless how the storage account was created.

Resources