简体   繁体   English

Azure PowerShell - 从所有订阅中获取 VM 使用情况

[英]Azure PowerShell - get VM usage from across all subscriptions

I want to list all the VMs that generate costs in a specific timeframe or billing period.我想列出在特定时间范围或计费周期内产生成本的所有 VM。

I managed to create this script to get me the desired output:我设法创建了这个脚本来获得我想要的输出:

$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

But this will give me the output only for the current subscription.但这只会给我当前订阅的输出。

How can I run on all the subscriptions that I have on the azure tenant?如何在 Azure 租户上的所有订阅上运行?

I tried using the below version but it doesn't seem to work:我尝试使用以下版本,但似乎不起作用:

$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

Any suggestions?有什么建议?

Mixing the Azure PowerShell module and Azure CLI could be causing issues with your code if the accounts haven't been retrieved between the two.如果未在两者之间检索帐户,则混合使用 Azure PowerShell 模块和 Azure CLI 可能会导致代码出现问题。 Verify that az cli has the proper subscriptions验证 az cli 是否具有正确的订阅

az account list -o table

If you don't see the accounts be sure to re-run az login .如果您没有看到帐户,请务必重新运行az login

Here's your code with the azure cli only这是您仅使用 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

never do += on an array, worst pattern ever.永远不要在数组上做+= ,有史以来最糟糕的模式。

[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)
 }
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM