【问题标题】:Azure PowerShell - get VM usage from across all subscriptionsAzure PowerShell - 从所有订阅中获取 VM 使用情况
【发布时间】:2021-02-14 09:21:02
【问题描述】:

我想列出在特定时间范围或计费周期内产生成本的所有虚拟机。

我设法创建了这个脚本来得到我想要的输出:

$file="C:\temp\GeneratedCost-short.csv"

(az consumption usage list `
--start-date "2020-07-01" --end-date "2020-07-31" | ConvertFrom-Json)`
| Where-Object {$_.product -Match "Virtual Machines"}`
| Sort-Object -Property instanceName -Descending | Select-Object instanceName, subscriptionName`
| Get-Unique -AsString | ConvertTo-Csv -NoTypeInformation | Set-Content $file

但这只会给我当前订阅的输出。

如何在 azure 租户上的所有订阅上运行?

我尝试使用以下版本,但它似乎不起作用:

$file="C:\temp\GeneratedCost-short.csv"

$VMs = @()
$Subscriptions = Get-AzSubscription
foreach ($sub in $Subscriptions) {
    Get-AzSubscription -SubscriptionName $sub.Name | az account set -s $sub.Name
    $VMs += (az consumption usage list --start-date "2020-07-01" --end-date "2020-07-03" | ConvertFrom-Json)
}
# 
$VMs | Where-Object {$_.product -Match "Virtual Machines"}`
| Sort-Object -Property instanceName -Descending | Select-Object instanceName, subscriptionName`
| Get-Unique -AsString | ConvertTo-Csv -NoTypeInformation | Set-Content $file

有什么建议吗?

【问题讨论】:

    标签: azure powershell azure-cli


    【解决方案1】:

    永远不要在数组上使用+=,这是有史以来最糟糕的模式。

    [System.Collections.Generic.List[PSObject]]$VMs = @()
    $subs = Get-AzSubscription # | Where-Object {$_.State -eq 'Enabled'}
    foreach ($s in $subs) {
      Set-AzContext -SubscriptionObject $s | Out-Null
      $vm = # your search here ...
      $VMs.Add($vm)
     }
      
    

    【讨论】:

      【解决方案2】:

      如果尚未在两者之间检索帐户,则混合使用 Azure PowerShell 模块和 Azure CLI 可能会导致代码出现问题。验证 az cli 是否有正确的订阅

      az account list -o table
      

      如果您没有看到帐户,请务必重新运行az login

      这是您仅使用 azure cli 的代码

      $file="C:\temp\GeneratedCost-short.csv"
      
      $VMs = @()
      az account list -o json | ConvertFrom-Json |
          ForEach-Object {
              Write-Host "Getting usage for account: " $_.Name
      az account set -s $_.Name
          $VMs += (az consumption usage list --start-date "2020-07-01" --end-date "2020-07-03" | ConvertFrom-Json)
          }
      
      $VMs | Where-Object {$_.product -Match "Virtual Machines"} |
          Sort-Object -Property instanceName -Descending |
              Select-Object instanceName, subscriptionName |
                  Get-Unique -AsString | ConvertTo-Csv -NoTypeInformation |
                      Set-Content $file
      

      【讨论】:

      • 我遇到了一些奇怪的事情,如果我运行上面版本的脚本,我会收到错误:Getting usage for subscription: Subscription1 Subscription2 UnrecognizedArgumentError: unrecognized arguments: Subscription2,它只从一个订阅中获取虚拟机。如果我多次运行脚本,它会随机更改将显示 VM 的订阅。如果我运行以下版本来获取订阅,则该错误不再存在并且它显示所有订阅,我将在下一条评论中写下它。非常感谢您的帮助!
      • $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 --start-date "2020-07-01" --end-date "2020-07-03" | ConvertFrom-Json) }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2019-03-27
      • 2020-05-26
      相关资源
      最近更新 更多