In this article, I am going to explain how to resize an Azure VM using PowerShell. You may need to do this for many reasons. You can perform this action via the Azure Portal, but you will be limited to one VM at a time. This is can be time-consuming.
Some things to know before we resize a VM is that if your VM is part of an Availability Set the VM and any others need to be in a stopped (deallocated) state. You may also find that if the resize option you want is not on the current hardware your VM is deployed to the VM will need to be switched off and it will be moved to different hardware. If you have a single VM you want to resize all you will have is one reboot if the resize option is available on the current hardware.
PowerShell Bits
Connect to Azure
Connect to Azure
Open PowerShell with elevated permissions.
Login-AzureRMAccount |
You may get a message about participating in some data collection. Select what option you want.
Enter your Azure Login Details in the Azure Login box.
You have now logged in to Azure using PowerShell.
Resize A VM
I only have one Subscription so it is easier for me. You can change subscription by using the Select-AzureRMSubscription PowerShell cmdlet
So let’s find out what VM’s I have running in this subscription.
Get-AzureRMVM |
Let’s say I want to resize PIXEL-TEST01. first I would find out what VM size I want to resize it to. You can find VM Sizes here.
I am going to resize PIXEL-TEST01 from Standard_A1 to the new Standard_A1_v2 VM size.
We can do that using this code.
$ARMVM = Get-AzureRmVM -VMName "PIXEL-TEST01" -ResourceGroupName "PIXELROBOTS.CO.UK" | |
$ARMVM.HardwareProfile.VmSize = "Standard_A1_v2" | |
Update-AzureRmVM -VM $ARMVM -ResourceGroupName "PIXELROBOTS.CO.UK" |
The Update-AzureRmVM actually restarts the VM.
Once the command has finished we can check to see if the resize has happened by using the following cmdlet.
Get-AzureRMVM |
As you can see PIXEL-TEST01 has been resized.
Resize Multiple VM’s
There are many ways to do this. I like to use a CSV file with a list of all my VM’s and their corresponding resource group and use a for each loop.
The below script can be used for this.
ForEach ($VM in $VMlist){ | |
$ARMVM = Get-AzureRmVM -VMName $VM.VMNAME -ResourceGroupName $VM.Rgroup | |
$ARMVM.HardwareProfile.VmSize = "Standard_A1_v2" | |
Update-AzureRmVM -VM $ArmVM -ResourceGroupName $VM.Rgroup} | |
Get-AzureRMVM |
And that’s it. You have now resized multiple Azure RM VM’s
I hope you found this blog post helpful. If you have any questions please leave a comment below.
0 Comments