如何合并Azure中的Azure RM资源和EA门户中的账单信息?

时间:2018-12-09 23:21:10

标签: azure powershell azure-resource-manager

下午好,

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

我编写了以下内容,该内容连接到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-使用连接对象后的脚本

       $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

2 个答案:

答案 0 :(得分:0)

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

但是,如果您可以共享一些样本数据集,将会提高生产力。

答案 1 :(得分:0)

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