【问题标题】:How can I combine Azure RM resources from Azure and their billing information from the EA portal?如何将 Azure 中的 Azure RM 资源与 EA 门户中的计费信息结合起来?
【发布时间】:2018-12-19 12:29:41
【问题描述】:

下午好,

我正在编写一个脚本,该脚本将输出资源组中每个资源的成本,然后通过电子邮件发送。

我写了如下,它连接到Azure并抓取每个资源组中的资源,输出如下表格式:

资源名称 |资源组 |资源类型 |位置

Example table/PSObject output

这是我想要的,但是我想添加当前计费月份的资源成本(尽管上个月也可以),但不完全确定如何执行此操作!我已经阅读了有关 UsageDetails 和 Billing API 的信息,但有点过头了,因此非常感谢您的指导!

    #requires -Version 3.0 -Modules ActiveDirectory, AzureRM
#Clear-Variable

function Connect-Azure
{

  Import-Module -Name AzureRM

  #Authenticate to Azure with Azure RM credentials

  Connect-AzureRmAccount

  #Select Azure Subscription
  Get-AzureRmSubscription|
  Out-GridView -PassThru|
  Set-AzureRmContext
}

Connect-Azure
$date = (Get-Date -Format d)
$ResourceGrps = Get-AzureRmResourceGroup

ForEach ($ResourceGrp in $ResourceGrps)

{
  $a = $ResourceGrp.ResourceGroupName

  $owner = 'ChevronX'

  $azureresources = Get-AzureRmResource |
  Where-Object -FilterScript {
    $_.ResourceGroupName -eq "$a" 
  } | 
  Select-Object -Property Name, ResourceGroupName, ResourceType, Location

  }

对于 EA 导出,我可以使用以下函数下载数据:

function Get-AzureBillingUsageDetails
{
[cmdletbinding()]
    param (
        [parameter(mandatory)]
        [string]$enrollmentNo,

        [parameter(mandatory)]
        [string]$accessKey
    )

    try {    

    Write-Verbose -Message "Checking Billing Period for the following Context:" -Verbose

    Write-Warning -Message "Please select Billing Period from pop up."

    $BillingPeriod = Get-AzureRmBillingPeriod | Where {$_.BillingPeriodEndDate -lt (get-date)} | Select-Object -First 1 |  Select -Property Name
    $BP = $BillingPeriod.Name

    $authHeaders = @{"authorization"="bearer $accessKey"}
    $usageUrl = "https://consumption.azure.com/v2/enrollments/$enrollmentNo/billingPeriods/$BP/usagedetails"

    Write-Verbose -Message "URL: $usageUrl" -Verbose
    Write-Verbose -Message "Selected Month, Billing Period: $BillingPeriod" -Verbose
    Write-Verbose -Message "Polling for Data, please wait..." -Verbose

        while ($usageUrl -ne $null) #1000 lines of usage data returned per request
        {
            $usagedetails = Invoke-RestMethod -Uri $usageUrl -Headers $authHeaders -ErrorAction Stop -Method GET
            $usagedetails.data
            $usageUrl = $usagedetails.nextLink
        }
    Write-Verbose -Message "Completed Polling for Data" -Verbose
    }
    Catch {
        Write-Warning $_
    }
}#Get-AzureBillUsageDetails

还有:

}#Get-AzureBillUsageDetails

function Get-AzureBillingMonthlySummary
{
 param (
        $usage,
    $Filter = '.'
)

end{
    [double]$gtotal = 0
    $usage | group resourcegroup | sort Name -Descending |
    Where Name -match $Filter |
    foreach-object {

    $g = $_.group
    $total = ($g | measure -Property cost -Sum | foreach Sum)

    $tags = ConvertFrom-Json $g[0].tags



        [pscustomobject]@{
         ResourceGroup    = $g[0].resourceGroup
            SubscriptionName = $g[0].subscriptionName

               BillTo       = $tags.BillTo
            'Application Owner' = $tags.'Application Owner'
            'Reference Name'      = $tags.'Reference Name'
            Environment = $tags.Environment
          #  DepartmentId     = $g[0].departmentId
            TotalCost        = "{0:C}" -f $total
    }

        $gtotal += $total
    }#Foreach-Object
Write-Verbose "Total is: $gtotal" -Verbose
}#End

}

我想直接在 Azure 中从资源组中获取标签,因为它们是最新的,并且有些资源组的资源不支持标记。这让我很沮丧,因为我对它的理解还不够,无法将数据合并在一起!希望有人能帮助我把它进一步结合起来。

--

11/12/18 - 使用 Join-Object 后的脚本

       $azureresources = Get-AzureRmResource |
        Where-Object -FilterScript {
        $_.ResourceGroupName -eq "$a"
    } |
        Select-Object -Property Name, ResourceGroupName, ResourceType, Location, ResourceId

       $report =  Join-Object -Left $azureresources -Right $usage -LeftJoinProperty 'ResourceId' -RightJoinProperty 'instanceId' -Type AllInLeft -RightProperties 'cost'

 $ourObject = @()
ForEach ($resource in $report)


{

 $resourcecost = ($report.cost |  measure -Sum | foreach Sum)

             $ourObject+= [pscustomobject] @{

'Resource Name' =  $resource.Name
'Resource Group'  =  $resource.ResourceGroupName
'Resource Type'  =  $resource.ResourceType
'Resource Cost'  =  "{0:C}" -f  $resourcecost


}

} 

Current script output - duplication of resources

【问题讨论】:

  • 我会在 resourceId 上合并
  • 这实际上听起来我想做的,你能给我一个可行的例子吗?是不是类似于:powershellgallery.com/packages/Join/2.3.0
  • 不抱歉,一个问题有点过分\answer
  • 谢谢 - 它正在接近。我最终添加了“加入对象”功能。但是现在我最终得到了多个资源,这些资源存在于 $report 中(出于某种原因),并且资源成本与资源不一致,您或任何人对解决此问题并仅从 $azureresources 中选择唯一资源有什么建议吗.我当前的脚本现在如下所示:

标签: azure powershell azure-resource-manager


【解决方案1】:

也许您可以尝试select -unique,以过滤掉重复项。

但如果您可以共享一些示例数据集,效率会更高。

【讨论】:

    【解决方案2】:

    感谢您的帮助/建议!设法整理出来。 感谢 '4c74356b41' 评论 - 设法使用 Join-Object 函数 (http://ramblingcookiemonster.github.io/Join-Object/) 将数据合并在一起。只需使用带有选项“-Type OnlyIfInBoth”的 Join-Object 即可消除重复项!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多