Reading Time: 3 minutes
Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

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

Open PowerShell with elevated permissions.

Login-AzureRMAccount
view raw Resize_AzureVM.ps1 hosted with ❤ by GitHub

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
view raw Resize_AzureVM.ps1 hosted with ❤ by GitHub

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"
view raw Resize_AzureVM.ps1 hosted with ❤ by GitHub

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
view raw Resize_AzureVM.ps1 hosted with ❤ by GitHub

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
view raw Resize_AzureVM.ps1 hosted with ❤ by GitHub

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.

Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email
Categories: AzurePowerShell

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

I agree