Update SKU size on next reboot.

Victor Day 20 Reputation points
2025-05-08T20:40:43.5466667+00:00

Is there a way to write a script that I can run that will update the SKU size of a virtual server on its next scheduled reboot? I am wanting to upgrade SKU from version 3 to version 5, an example would be E4s_v3 to E4ds_v5, is there a way I can automate this? There are several hundred servers that I want to update but don't want to do it manually 1 at a time. These servers also cannot be randomly shut off to accommodate the SKU upgrade, they are on a set reboot schedule scattered throughout the month. My ultimate goal is to get a script together that will see when the server is rebooting and apply the SKU update. Thanks in advance.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,831 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Venkat V 2,065 Reputation points Microsoft External Staff Moderator
    2025-05-09T05:57:48.5166667+00:00

    Hi @Victor Day

    You can create a scheduled Runbook in the Azure Automation Account and set the schedule to match the server's known reboot time. This way, the Runbook will trigger the script at the same time. If the VM state matches "deallocated", it will automatically update the SKU.

    This approach helps avoid unnecessary reboots for VM SKU changes across all VMs.

    $resourceGroup = "MyRG"
    $targetSku = "E4ds_v5"
    # Get all VMs in the resource group
    $vms = Get-AzVM -ResourceGroupName $resourceGroup
    foreach ($vm in $vms) {
        $vmStatus = Get-AzVM -Name $vm.Name -ResourceGroupName $resourceGroup -Status
        if ($vmStatus.PowerState -eq "VM deallocated") {
            if ($vm.HardwareProfile.VmSize -ne $targetSku) {
                Write-Output "Resizing VM '$($vm.Name)' to SKU '$targetSku'..."
                $vm.HardwareProfile.VmSize = $targetSku
                Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
                Write-Output "VM '$($vm.Name)' resized successfully."
            } else {
                Write-Output "VM '$($vm.Name)' already has the desired SKU '$targetSku'. Skipping."
            }
        } else {
            Write-Output "VM '$($vm.Name)' is not deallocated. Skipping."
        }
    }
    

    This way you avoid rebooting for VM SKU changes for all the VMs.

    Reference: Create a standalone Azure Automation account
    Change the size of a virtual machine

    I hope this helps to resolve your query.


    If the above is unclear and/or you are unsure about something, add a comment below.

    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.