Skip to content

Instantly share code, notes, and snippets.

@marckean
Last active November 5, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marckean/657abe4c3f5468d8f4878b07002bc5cd to your computer and use it in GitHub Desktop.
Save marckean/657abe4c3f5468d8f4878b07002bc5cd to your computer and use it in GitHub Desktop.
# 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
Copy link

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
Copy link

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