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:
- Create SNAPSHOTS of existing data disks from source VM
- Create new DATADISKS from the SNAPSHOTS created from step 1
- 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
add a comment |
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:
- Create SNAPSHOTS of existing data disks from source VM
- Create new DATADISKS from the SNAPSHOTS created from step 1
- 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
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
add a comment |
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:
- Create SNAPSHOTS of existing data disks from source VM
- Create new DATADISKS from the SNAPSHOTS created from step 1
- 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
I am developing a script to copy data disks from one Azure VM to another Azure VM. Below is the task:
- Create SNAPSHOTS of existing data disks from source VM
- Create new DATADISKS from the SNAPSHOTS created from step 1
- 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
azure azure-virtual-machine azure-powershell
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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