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

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

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

示例脚本Sample script

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

# Get managed applications from known resource group
Get-AzManagedApplication -ResourceGroupName "DemoApp"

# Get ID of managed resource group
(Get-AzManagedApplication -ResourceGroupName "DemoApp").Properties.managedResourceGroupId

# Get virtual machines in the managed resource group
Get-AzResource -ResourceGroupName DemoApp6zkevchqk7sfq -ResourceType Microsoft.Compute/virtualMachines

# Get information about virtual machines in managed resource group
Get-AzVM -ResourceGroupName DemoApp6zkevchqk7sfq | ForEach{ $_.Name, $_.storageProfile.osDisk.osType, $_.hardwareProfile.vmSize }

## Resize virtual machines in managed resource group
$vm = Get-AzVM -ResourceGroupName DemoApp6zkevchqk7sfq -VMName demoVM
$vm.HardwareProfile.VmSize = "Standard_D2_v2"
Update-AzVM -VM $vm -ResourceGroupName DemoApp6zkevchqk7sfq

脚本说明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
Get-AzManagedApplicationGet-AzManagedApplication 列出托管应用程序。List managed applications. 提供要重点关注结果的资源组名称。Provide resource group name to focus the results.
Get-AzResourceGet-AzResource 列出资源。List resources. 提供要重点关注结果的资源组和资源类型。Provide a resource group and resource type to focus the result.
Update-AzVMUpdate-AzVM 更新虚拟机的大小。Update a virtual machine's size.

后续步骤Next steps