重新启动 VMRestart VMs

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

此示例展示了用来获取一些 VM 并重新启动它们的几种方法。This sample shows a couple of ways to get some VMs and restart them.

第一种方法重新启动资源组中的所有 VM。The first restarts all the VMs in the resource group.

az vm restart --ids $(az vm list --resource-group myResourceGroup --query "[].id" -o tsv)

第二种方法使用 az resource list 获取带标记的 VM,并筛选出是 VM 的资源,然后重新启动那些 VM。The second gets the tagged VMs using az resource list and filters to the resources that are VMs, and restarts those VMs.

az vm restart --ids $(az resource list --tag "restart-tag" --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o tsv)

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install Azure CLI on Windows.

示例脚本Sample script

此示例具有三个脚本。The sample has three scripts. 第一个脚本用来预配虚拟机。The first one provisions the virtual machines. 它使用了 no-wait 选项,因此,命令不会等待每个 VM 完成部署便会返回。It uses the no-wait option so the command returns without waiting on each VM to be provisioned. 第二个脚本等到 VM 完全部署后才会返回。The second waits for the VMs to be fully provisioned. 第三个脚本重新启动已部署的所有 VM,并仅重新启动带标记的 VM。The third script restarts all the VMs that were provisioned, and then just the tagged VMs.

预配 VMProvision the VMs

此脚本创建一个资源组,并它创建三个 VM 并重新启动。This script creates a resource group and then it creates three VMs to restart. 其中的两个带有标记。Two of them are tagged.

#!/bin/bash

# Create a resource group where we'll create the VMs that we'll start
az group create -n myResourceGroup -l chinanorth

# Create the VMs. Two are tagged and one is not. --generated-ssh-keys will create ssh keys if not present
az vm create -g myResourceGroup -n myVM1 --image UbuntuLTS --admin-username deploy --tags "restart-tag" --generate-ssh-keys --no-wait
az vm create -g myResourceGroup -n myVM2 --image UbuntuLTS --admin-username deploy --tags "restart-tag" --no-wait
az vm create -g myResourceGroup -n myVM3 --image UbuntuLTS --admin-username deploy --no-wait

等待Wait

此脚本每隔 20 秒检查一次预配状态,直到三个 VM 全部完成预配,或者其中一个未能完成预配。This script checks on the provisioning status every 20 seconds until all three VMs are provisioned, or one of them fails to provision.

#!/bin/bash

# Wait for the VMs to be provisioned
while [[ $(az vm list --resource-group myResourceGroup --query "length([?provisioningState=='Succeeded'])") != 3 ]]; do
    echo "The VMs are still not provisioned. Trying again in 20 seconds."
    sleep 20
    if [[ $(az vm list --resource-group myResorceGroup --query "length([?provisioningState=='Failed'])") != 0 ]]; then
        echo "At least one of the VMs failed to be provisioned."
        exit 1
    fi
done
echo "The VMs are provisioned."

重新启动 VMRestart the VMs

此脚本重新启动资源组中的所有 VM,它仅重新启动带标记的 VM。This script restarts all the VMs in the resource group, and then it restarts just the tagged VMs.

#!/bin/bash

# Get the IDs of all the VMs in the resource group and restart those
az vm restart --ids $(az vm list --resource-group myResourceGroup --query "[].id" -o tsv)

# Get the IDs of the tagged VMs and restart those
az vm restart --ids $(az resource list --tag "restart-tag" --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o tsv)

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组、VM 以及所有相关的资源。After the script sample has been run, the following command can be used to remove the resource groups, VMs, and all related resources.

az group delete -n myResourceGroup --no-wait --yes

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机、可用性集、负载均衡器和所有相关资源。This script uses the following commands to create a resource group, virtual machine, availability set, load balancer, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az vm createaz vm create 创建虚拟机。Creates the virtual machines.
az vm listaz vm list --query 一起使用,用来确保在重新启动 VM 之前已对其进行了预配,获取这些 VM 的 ID 以将其重新启动。Used with --query to ensure the VMs are provisioned before restarting them, and then to get the IDs of the VMs to restart them.
az resource listaz resource list --query 一起使用来获取使用该标记的 VM 的 ID。Used with --query to get the IDs of the VMs using the tag.
az vm restartaz vm restart 重新启动 VM。Restarts the VMs.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.