azure 基于管理组名称计算虚拟机数量

enxuqcxy  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(137)

我想创建一个Graph QL查询或Powershell脚本(首选),以显示所有Azure订阅中的VM总数沿着包含特定字符串的管理组中的VM数量,以及不包含该特定字符串的管理组下的其余VM的计数

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| Count
| project TotalVMs=Count
iyr7buue

iyr7buue1#

所有Azure订阅中的VM总数沿着包含特定字符串的管理组中的VM数,以及管理组中不包含该特定字符串的其余VM的计数。
我已经遵循了这个MS Doc1MSDoc2作为参考。
这里是Powershell script,显示所有Azure订阅VM的总数,沿着包含特定字符串的management groupsVM的数量。

> 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"

输出:

相关问题