使用 Azure CLI 获取托管资源组中的资源并重设 VM 大小Get resources in a managed resource group and resize VMs with Azure CLI

此脚本从托管资源组中检索资源,并重设该资源组中 VM 的大小。This script retrieves resources from a managed resource group, and resizes the VMs in that resource group.

若要运行此示例,请安装最新版本的 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 Subscription before you begin.

示例脚本Sample script

#!/bin/bash

# Get managed applications from known resource group
az managedapp list --query "[?contains(resourceGroup,'DemoApp')]"

# Get ID of managed resource group
az managedapp list --query "[?contains(resourceGroup,'DemoApp')].{ managedResourceGroup:managedResourceGroupId }"

# Get virtual machines in the managed resource group
az resource list -g DemoApp6zkevchqk7sfq --query "[?contains(type,'Microsoft.Compute/virtualMachines')]"

# Get information about virtual machines in managed resource group
az vm list -g DemoApp6zkevchqk7sfq --query "[].{VMName:name,OSType:storageProfile.osDisk.osType,VMSize:hardwareProfile.vmSize}"

## Resize virtual machines in managed resource group
az vm resize --size Standard_D2_v2 --ids $(az vm list -g DemoApp6zkevchqk7sfq --query "[].id" -o tsv)

脚本说明Script explanation

此脚本使用以下命令部署托管应用程序。This script uses the following commands to deploy the managed application. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command-specific documentation.

CommandCommand 说明Notes
az managedapp listaz managedapp list 列出托管应用程序。List managed applications. 提供要重点关注结果的查询值。Provide query values to focus the results.
az resource listaz resource list 列出资源。List resources. 提供要重点关注结果的资源组和查询值。Provide a resource group and query values to focus the result.
az vm resizeaz vm resize 更新虚拟机的大小。Update a virtual machine's size.

后续步骤Next steps