使用 PowerShell 处理计划内维护Handling planned maintenance using PowerShell

本文适用于同时运行 Linux 和 Windows 的虚拟机。This article applies to virtual machines running both Linux and Windows.

可以使用 Azure PowerShell 查看何时安排 VM 进行维护You can use Azure PowerShell to see when VMs are scheduled for maintenance. 使用 -status 参数时可通过 Get-AzVM cmdlet 获得计划内维护信息。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. 如果未计划任何影响 VM 的维护,该 cmdlet 不返回任何维护信息。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 

在 MaintenanceRedeployStatus 下返回以下属性:The following properties are returned under MaintenanceRedeployStatus:

ValueValue 说明Description
IsCustomerInitiatedMaintenanceAllowedIsCustomerInitiatedMaintenanceAllowed 指示此时是否可以在 VM 上启动维护Indicates whether you can start maintenance on the VM at this time
PreMaintenanceWindowStartTimePreMaintenanceWindowStartTime 可以在 VM 上启动维护的自助式维护时段的起点The beginning of the maintenance self-service window when you can initiate maintenance on your VM
PreMaintenanceWindowEndTimePreMaintenanceWindowEndTime 可以在 VM 上启动维护的自助式维护时段的终点The end of the maintenance self-service window when you can initiate maintenance on your VM
MaintenanceWindowStartTimeMaintenanceWindowStartTime Azure 在 VM 上启动维护的计划内维护时段的起点The beginning of the maintenance scheduled in which Azure initiates maintenance on your VM
MaintenanceWindowEndTimeMaintenanceWindowEndTime Azure 在 VM 上启动维护的计划内维护时段的终点The end of the maintenance scheduled window in which Azure initiates maintenance on your VM
LastOperationResultCodeLastOperationResultCode 上次尝试在 VM 上启动维护的结果The result of the last attempt to initiate maintenance on the VM

还可以通过使用 Get-AzVM 并且不指定 VM 来获取资源组中所有 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

以下 PowerShell 示例获取订阅 ID,并返回计划进行维护的 VM 列表。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)"               
            }
          }
    }
}

使用 PowerShell 在 VM 上启动维护Start maintenance on your VM using PowerShell

如果 IsCustomerInitiatedMaintenanceAllowed 设置为 true,以下命令使用上一部分中函数的信息,在 VM 上启动维护。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

重要

经典 VM 将于 2023 年 3 月 1 日停用。Classic VMs will be retired on March 1, 2023.

如果从 ASM 使用 IaaS 资源,请在 2023 年 3 月 1 日之前完成迁移。If you use IaaS resources from ASM, please complete your migration by March 1, 2023. 我们建议你尽快进行切换,以利用 Azure 资源管理器中的许多增强功能。We encourage you to make the switch sooner to take advantage of the many feature enhancements in Azure Resource Manager.

有关详细信息,请参阅在 2023 年 3 月 1 日之前将 IaaS 资源迁移到 Azure 资源管理器For more information, see Migrate your IaaS resources to Azure Resource Manager by March 1, 2023.

如果你仍在使用由经典部署模型部署的旧 VM,则可以使用 PowerShell 查询 VM,并启动维护。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.

若要获取 VM 的维护状态,请键入:To get the maintenance status of a VM, type:

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

若要在经典 VM 上启动维护,请键入:To start maintenance on your classic VM, type:

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

后续步骤Next steps

还可以使用 Azure CLI门户处理计划内维护。You can also handle planned maintenance using the Azure CLI or portal.