使用 PowerShell 获取托管资源组中的资源并重设 VM 大小

此脚本从托管资源组中检索资源,并重设该资源组中 VM 的大小。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

示例脚本

# 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

脚本说明

此脚本使用以下命令部署托管应用程序。 表中的每条命令链接到特定于命令的文档。

命令 说明
Get-AzManagedApplication 列出托管应用程序。 提供要重点关注结果的资源组名称。
Get-AzResource 列出资源。 提供要重点关注结果的资源组和资源类型。
Update-AzVM 更新虚拟机的大小。

后续步骤