根据管理组名称计算虚拟机数量

问题描述 投票:0回答:1

我想创建一个 Graph QL 查询或 Powershell 脚本(首选),以显示所有 Azure 订阅中的虚拟机总数以及包含特定字符串的管理组中的虚拟机数量和管理中的其余虚拟机数量没有特定字符串的组

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| Count
| project TotalVMs=Count
azure powershell azure-powershell azure-resource-graph
1个回答
0
投票

所有 Azure 订阅中的 VM 总数以及包含特定字符串的管理组中的 VM 数量以及管理组中不具有该特定字符串的其余 VM 的数量。

我已经按照这个 MS Doc1 & MSDoc2 作为参考。

这里是

Powershell script
显示所有Azure订阅中的VM总数以及management groups中包含特定字符串的
VM
数量。

> Connect-AzAccount 
> Select-AzSubscription -All 
> $searchString = "vm"
> $mgmtGroups = Get-AzManagementGroup
> $totalVMs = 0 
> $matchingVMs = 0 
> $nonMatchingVMs = 0
> foreach ($mgmtGroup in $mgmtGroups) {
> Write-Host "Checking management group $($mgmtGroup.DisplayName)"
>     $vms = Get-AzResource -ResourceType "Microsoft.Compute/virtualMachines"
>     $vms.Name
>     $vmCount = $vms.Count
>     $totalVMs += $vmCount
>     if ($vmCount -gt 0) {
>         $matchingVMCount = ($vms | Where-Object { $_.Name -like "*$searchString*" }).Count
>         $matchingVMs += $matchingVMCount
>         $nonMatchingVMs += ($vmCount - $matchingVMCount)
>     }
>  Write-Host "Found $vmCount VMs in the management group"
>  Write-Host "Found $matchingVMCount VMs containing '$searchString'" }

> Write-Host "Total VMs in all subscriptions: $totalVMs" Write-Host
> "Matching VMs in Management Group: $matchingVMs" Write-Host
> "Non-matching VMs in Management Group: $nonMatchingVMs"

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.