下午好,

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

我写了以下内容,它连接到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