azure - Count VMs by resource groups when multiple subscriptions
问题描述
I've created a PowerShell script that will export to an Excel file details about components that generated costs in Azure.
In one tab named "Details" there are exported all the components that generated costs in a specific billing period. And in another tab named "Short" there is exported count of all the VMs that generated costs filtered by subscriptions.
I tried in multiple ways but couldn't wrap my head around a new request.
How can I count all the VMs that generated costs filtered by resource groups. If you can help I would much appreciate it.
Cheers!
If (-not(Get-InstalledModule ImportExcel -ErrorAction silentlycontinue)) {
Write-Output "Module does not exist"
}
Else {
Write-Output "Module ImportExcel exists. Continuing with gathering data."
}
$file=".\resources.xlsx"
# Because Get-AzBilling gives us the value as yyyyMM we need also a value for day.
# Used $billingperiodfinal to generate the correct value to be used.
$billingperiod = (Get-AzBillingPeriod)[1].Name
$day = "01"
$billingperiodfinal = $billingperiod+$day
$date=Get-Date -format "yyy-MM-dd"
$time2=(Get-Date).addmonths(-1)
$date2=Get-Date $time2 -format "yyy-MM"
$VMs = @()
$Subscriptions = Get-AzSubscription
foreach ($sub in $Subscriptions) {
Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
az account set -s $sub.Name
$VMs += (az consumption usage list -p $billingperiodfinal | ConvertFrom-Json)
}
$VMsDetails = $VMs
$VMsDetails | Export-Excel -path $file -ClearSheet -workSheetName "Detailed"
$VMsShort = $VMs `
| Where-Object {($_.product -Match "Virtual Machines")} `
| Sort-Object -Property instanceName -Descending `
| Select-Object instanceName, subscriptionName `
| Get-Unique -AsString `
| Group-Object subscriptionName | Select-Object Name,Count `
| Select-Object @{ expression={$_.Name}; label='Subscription Name' }, @{ expression={$_.Count}; label='Number of VMs' }
$VMsShort | Export-Excel -path $file -ClearSheet -workSheetName "Short"
解决方案
I swapped az consumption usage list
for Get-AzConsumptionUsageDetail because I can see the output on their help page, and I know it will take ResourceGroup
as a parameter. This suggestion to iterate through your groups and add the group name as a property should work either way though:
$Subscriptions = Get-AzSubscription
# Iterate through subs
$AzConsumption = foreach ($sub in $Subscriptions) {
Set-AzContext $sub
$AzResourceGroups = Get-AzResourceGroup | sort ResourceGroupName
# Iterate through each resource group
Foreach ($rg in $AzResourceGroups) {
# Get the consumption usage - this can be replaced with the 'az consumption usage list' command
$RgConsumption = Get-AzConsumptionUsageDetail -ResourceGroup $rg.ResourceGroupName -BillingPeriodName (Get-AzBillingPeriod)[1].Name
# Add ResourceGroup as a property to consumption report.
$RgConsumption | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $rg.ResourceGroupName
$RgConsumption
}
}
$AzConsumptionShort = $AzConsumption |
Where Product -Match "Virtual Machines" |
Sort InstanceName -Descending |
Select InstanceName,SubscriptionName,ResourceGroup -Unique |
Group subscriptionName |
Select @{l='Subscription Name';e={$_.Name}},@{l='Number of VMs';e={$_.Count}}
# etc.
推荐阅读
- c# - 如何使用在kafka中发送的最新文本文件数据并使用c#写回另一个文本文件?
- c# - ASP.NET 中的 MVC - 使用 IEnumerable 显示值而不是 Id
- html - 解码 Html 实体
- python - 有没有办法检查 PyQt5 窗口是否打开并相应地延迟另一个实例?
- xml - 是否可以为 VS Code 中的某些 XML 标签分配不同的颜色?
- ios - 使用 url 方案将 .ovpn 配置文件从 iOS 应用程序发送到官方 openVPN 客户端
- apache-spark - 从包含大量文件的 s3 存储桶读取时,AWS Glue 作业在 create_dynamic_frame_from_options 失败
- ssh - 如何从 drud/ddev-ssh-agent 容器中删除密钥?
- android - 如何通过 firebase curl 发送短信
- r - 根据数据框的两个变量给出排名