Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Recently tested fully with a later version of the Azure PowerShell module - Install-Module AzureRM -RequiredVersion 4.4.1
#region Logon to Azure
Login-AzureRmAccount
$subscription = Get-AzureRmSubscription | Out-GridView -PassThru
Select-AzureRmSubscription -SubscriptionId $subscription.Id
#endregion
# Variables for YOu to fill in
$ResourceGroup = 'NestedVMware' # resource group name to contain the new NIC
$VMname = 'VMware-01' # name of the VM you want to swap out the OS disk
#Get the VM config to a variable
$VM = Get-AzureRmVM -Name $VMname -ResourceGroupName $ResourceGroup
#Stop and deallocate the VM
Stop-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -Force
#swap out the OS disk using the VM variable
$VM.StorageProfile.OsDisk.Vhd.Uri = 'https://storage5142161532.blob.core.windows.net/vhds/0120171108081251.vhd'
#Update the VM configuration
Update-AzureRmVM -VM $VM -ResourceGroupName $ResourceGroup
@thebitguru

This comment has been minimized.

Copy link

@thebitguru thebitguru commented Mar 11, 2018

FYI, it seems that this is no longer allowed. Here is the error that I get when I follow this.

Update-AzureRmVM : Changing property 'osDisk.vhd.uri' is not allowed.
ErrorCode: PropertyChangeNotAllowed
ErrorMessage: Changing property 'osDisk.vhd.uri' is not allowed.
StatusCode: 409
ReasonPhrase: Conflict
@dhaugli

This comment has been minimized.

Copy link

@dhaugli dhaugli commented Nov 5, 2019

FYI, it seems that this is no longer allowed. Here is the error that I get when I follow this.

Update-AzureRmVM : Changing property 'osDisk.vhd.uri' is not allowed.
ErrorCode: PropertyChangeNotAllowed
ErrorMessage: Changing property 'osDisk.vhd.uri' is not allowed.
StatusCode: 409
ReasonPhrase: Conflict

I managed to get this to work, but I needed to update it, from the original:

This assumes you are logged in to Azure and have selected the correct subscription upfront, you have the new Az Powershell module with backward compatability to AzureRM enabled.

$ResourceGroup = 'rgname'
$VMname = 'vmname'

$VM = Get-AzVM -Name $VMname -ResourceGroupName $ResourceGroup
$VM.StorageProfile.OsDisk.Vhd.Uri = 'storageaccountURLtoVhd'
Update-AzureRmVM -VM $VM -ResourceGroupName $ResourceGroup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.