PowerShell script to move data disk from one azure vm to another vm











up vote
0
down vote

favorite












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









share|improve this question
























  • You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
    – Rthomas529
    Nov 21 at 14:52















up vote
0
down vote

favorite












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









share|improve this question
























  • You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
    – Rthomas529
    Nov 21 at 14:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question















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






azure azure-virtual-machine azure-powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 17:12









James Z

11.1k71735




11.1k71735










asked Nov 21 at 14:08









Manjunath Rao

147112




147112












  • You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
    – Rthomas529
    Nov 21 at 14:52


















  • You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
    – Rthomas529
    Nov 21 at 14:52
















You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
– Rthomas529
Nov 21 at 14:52




You can't change the disk name. Maybe reference these links. stackoverflow.com/questions/47759200/… github.com/Azure/azure-quickstart-templates/tree/master/…
– Rthomas529
Nov 21 at 14:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer





















  • 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 at 15:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413895%2fpowershell-script-to-move-data-disk-from-one-azure-vm-to-another-vm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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.






share|improve this answer





















  • 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 at 15:00















up vote
1
down vote



accepted










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.






share|improve this answer





















  • 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 at 15:00













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 15:31









Charles Xu

2,823126




2,823126












  • 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 at 15:00


















  • 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 at 15:00
















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 at 15:00




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 at 15:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413895%2fpowershell-script-to-move-data-disk-from-one-azure-vm-to-another-vm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Sphinx de Gizeh