Azure 多个订阅时按资源组统计VM

Azure 多个订阅时按资源组统计VM,azure,powershell,azure-powershell,Azure,Powershell,Azure Powershell,我已经创建了一个PowerShell脚本,该脚本将导出到Excel文件,其中包含Azure中产生成本的组件的详细信息 在一个名为“详细信息”的选项卡中,将导出在特定计费周期内产生成本的所有组件。 在另一个名为“Short”的选项卡中,有生成按订阅过滤的成本的所有VM的导出计数 我尝试了多种方法,但都无法满足新的要求 如何计算按资源组筛选产生成本的所有VM。 如果你能帮忙,我将不胜感激 干杯 If (-not(Get-InstalledModule ImportExcel -ErrorAction

我已经创建了一个PowerShell脚本,该脚本将导出到Excel文件,其中包含Azure中产生成本的组件的详细信息

在一个名为“详细信息”的选项卡中,将导出在特定计费周期内产生成本的所有组件。 在另一个名为“Short”的选项卡中,有生成按订阅过滤的成本的所有VM的导出计数

我尝试了多种方法,但都无法满足新的要求

如何计算按资源组筛选产生成本的所有VM。 如果你能帮忙,我将不胜感激

干杯

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"

我将
az消费使用情况列表
替换为,因为我可以在他们的帮助页面上看到输出,并且我知道它将以
ResourceGroup
作为参数。不过,建议您迭代组并将组名添加为属性,这两种方法都可以:

$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.

ResourceGroup
是列表中的属性吗
$VMs
,还是您需要从其他地方获取它?很遗憾,$VMs的输出中没有显示它谢谢您的帮助!这就解决了问题。