0

I am developing a script to copy data disks from one Azure VM to another Azure VM. Below is the task:

  1. Create SNAPSHOTS of existing data disks from source VM
  2. Create new DATADISKS from the SNAPSHOTS created from step 1
  3. Attach the new DATADISKS to the destination VM

I have written the complete code. However Step 3 is throwing an error.

## Create Snapshot from a Managed Disk ##

$resourceGroupName = 'manju_copy_disk' 
$location = 'east us 2' 
$source_vm_name = 'server1'
$destination_vm_name = 'server3'

$source_vm_object = get-azurermvm -ResourceGroupName $resourceGroupName -Name $source_vm_name

$data_disk_list = Get-AzureRmDisk | where {$_.ManagedBy -match $source_vm_name -and $_.OsType -eq $null}

$snapshot_list = New-Object System.Collections.ArrayList($null)

foreach($data_disk_list_iterator in $data_disk_list){

    $snapshotName = $destination_vm_name + "_Snapshot_" + $data_disk_list_iterator.Name

    $snapshot_config = New-AzureRmSnapshotConfig -SourceUri $data_disk_list_iterator.id -Location $location -CreateOption copy

    $snapshot_object = New-AzureRmSnapshot -Snapshot $snapshot_config -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName

    $snapshot_list.Add($snapshot_object.Id)

}


## Create Managed disk from snap shot created above ##
$storageType = 'StandardLRS'


$count=0

$destination_datadisk_list = New-Object System.Collections.ArrayList($null)
#$destination_datadisk_Name_list = New-Object System.Collections.ArrayList($null)

foreach($snapshot_list_iterator in $snapshot_list){


    $disk_name = $destination_vm_name + "_datadisk_" + $count
    $count += 1

    $diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot_list_iterator

    $datadisk_object = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $disk_name

    $destination_datadisk_ID_list.Add($datadisk_object.Id)

}



## Attach Managed disk to destination vm

$destination_vm_object = Get-AzureRmVM -Name $destination_vm_name -ResourceGroupName $resourceGroupName 

$lun_count = 1

foreach($destination_datadisk_list_iterator in $destination_datadisk_list){
    $destination_datadisk_name = $destination_vm_name + "_datadisk_"+$lun_count
    $destination_vm_object = Add-AzureRmVMDataDisk -VM $destination_vm_object -Name $destination_datadisk_name -CreateOption Attach -ManagedDiskId $destination_datadisk_list_iterator -Lun $lun_count
    $lun_count += 1
}

Update-AzureRmVM -VM $destination_vm_object -ResourceGroupName $resourceGroupName  ## --> LINE CODE NOT WORKING

Below is the error:

Update-AzureRmVM : Changing property 'dataDisk.name' is not allowed.
ErrorCode: PropertyChangeNotAllowed
ErrorMessage: Changing property 'dataDisk.name' is not allowed.
StatusCode: 409
ReasonPhrase: Conflict
OperationID : e8a0a8de-0cdd-4ba0-90bc-883d37e374af
At line:1 char:1
+ Update-AzureRmVM -VM $destination_vm_object -ResourceGroupName $resou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Update-AzureRmVM], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.UpdateAzureVMCommand
James Z
  • 12,209
  • 10
  • 24
  • 44
Manjunath Rao
  • 1,397
  • 4
  • 26
  • 42
  • You can't change the disk name. Maybe reference these links. https://stackoverflow.com/questions/47759200/creating-a-managed-disk-from-snapshot-in-different-region-azure https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image –  Nov 21 '18 at 14:52

1 Answers1

1

When you attach the data disks which you create from the snapshot, you can not change the name again. Take a look at this.

So I suggest you can create the data disk from the snapshot and attach them to the destination VM in the same foreach loop with the same names.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • I removed the "-name" parameter from the "Add-AzureRmVMDataDisk" cmdlet and it worked. Also, I moved the code to attach the disks under the for loop as you suggested. Thanks – Manjunath Rao Nov 22 '18 at 15:00