Handling planned maintenance using PowerShell

Applies to: ✔️ Linux VMs ✔️ Windows VMs ✔️ Flexible scale sets ✔️ Uniform scale sets

You can use Azure PowerShell to see when VMs are scheduled for maintenance. Planned maintenance information is available from the Get-AzVM cmdlet when you use the -status parameter.

Maintenance information is returned only if there is maintenance planned. If no maintenance is scheduled that impacts the VM, the cmdlet does not return any maintenance information.

Get-AzVM -ResourceGroupName myResourceGroup -Name myVM -Status

Output

MaintenanceRedeployStatus               : 
  IsCustomerInitiatedMaintenanceAllowed : True
  PreMaintenanceWindowStartTime         : 5/14/2018 12:30:00 PM
  PreMaintenanceWindowEndTime           : 5/19/2018 12:30:00 PM
  MaintenanceWindowStartTime            : 5/21/2018 4:30:00 PM
  MaintenanceWindowEndTime              : 6/4/2018 4:30
  LastOperationResultCode               : None 

The following properties are returned under MaintenanceRedeployStatus:

Value Description
IsCustomerInitiatedMaintenanceAllowed Indicates whether you can start maintenance on the VM at this time
PreMaintenanceWindowStartTime The beginning of the maintenance self-service window when you can initiate maintenance on your VM
PreMaintenanceWindowEndTime The end of the maintenance self-service window when you can initiate maintenance on your VM
MaintenanceWindowStartTime The beginning of the maintenance scheduled in which Azure initiates maintenance on your VM
MaintenanceWindowEndTime The end of the maintenance scheduled window in which Azure initiates maintenance on your VM
LastOperationResultCode The result of the last attempt to initiate maintenance on the VM

You can also get the maintenance status for all VMs in a resource group by using Get-AzVM and not specifying a VM.

Get-AzVM -ResourceGroupName myResourceGroup -Status

The following PowerShell example takes your subscription ID and returns a list of VMs that are scheduled for maintenance.


function MaintenanceIterator
{
    Select-AzSubscription -SubscriptionId $args[0]

    $rgList= Get-AzResourceGroup 

    for ($rgIdx=0; $rgIdx -lt $rgList.Length ; $rgIdx++)
    {
        $rg = $rgList[$rgIdx]        
	$vmList = Get-AzVM -ResourceGroupName $rg.ResourceGroupName 
        for ($vmIdx=0; $vmIdx -lt $vmList.Length ; $vmIdx++)
        {
            $vm = $vmList[$vmIdx]
            $vmDetails = Get-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status
              if ($vmDetails.MaintenanceRedeployStatus )
            {
                Write-Output "VM: $($vmDetails.Name)  IsCustomerInitiatedMaintenanceAllowed: $($vmDetails.MaintenanceRedeployStatus.IsCustomerInitiatedMaintenanceAllowed) $($vmDetails.MaintenanceRedeployStatus.LastOperationMessage)"               
            }
          }
    }
}

Start maintenance on your VM using PowerShell

Using information from the function in the previous section, the following starts maintenance on a VM if IsCustomerInitiatedMaintenanceAllowed is set to true.

Restart-AzVM -PerformMaintenance -name $vm.Name -ResourceGroupName $rg.ResourceGroupName 

Classic deployments

Important

VMs created through the classic deployment model will be retired on March 1, 2023.

If you use IaaS resources from Azure Service Management, please complete your migration by March 1, 2023. We encourage you to make the switch sooner to take advantage of the many feature enhancements in Azure Resource Manager.

For more information, see Migrate your IaaS resources to Azure Resource Manager by March 1, 2023.

If you still have legacy VMs that were deployed using the classic deployment model, you can use PowerShell to query for VMs and initiate maintenance.

To get the maintenance status of a VM, type:

Get-AzureVM -ServiceName <Service name> -Name <VM name>

To start maintenance on your classic VM, type:

Restart-AzureVM -InitiateMaintenance -ServiceName <service name> -Name <VM name>

Next steps

You can also handle planned maintenance using the Azure CLI or portal.