azure - 如何将 Azure 中的 Azure RM 资源与 EA 门户中的计费信息结合起来?
问题描述
下午好,
我正在编写一个脚本,该脚本将输出资源组中每个资源的成本,然后将通过电子邮件发送。
我写了以下,它连接到 Azure 并抓取每个资源组中的资源,输出是如下表格式:
资源名称 | 资源组 | 资源类型 | 地点
这可以满足我的要求,但是我想添加当前计费月份的资源成本(尽管上个月也可以)并且不完全确定如何执行此操作!我已经阅读了有关 UsageDetails 和 Billing APIs 的内容,但有点过头了,因此非常感谢您的指导!
#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
}
}
解决方案
感谢您的帮助/建议!设法整理出来。感谢 '4c74356b41' 评论 - 设法使用 Join-Object 函数 ( http://ramblingcookiemonster.github.io/Join-Object/ ) 将数据合并在一起。只需使用带有选项“-Type OnlyIfInBoth”的 Join-Object 即可消除重复项!
推荐阅读
- flutter - Flutter Eddystone 信标检测
- list - 将字典中的键与值匹配,并使用 ansible 替换与另一个字典匹配的值
- javascript - 反应 setState 未将属性设置为文件值
- node.js - Promise.allSettled 不是节点 14.0.0 的函数
- python-3.x - 循环多个元组作为另一个函数的输入
- python - Pyside6 中的信号
- regex - 点之间数字的正则表达式
- r - 有没有办法让这个相关图看起来“更漂亮”?
- php - 你如何在 Codeigniter 4 的 $this->db->table('table_name') 中编写/调用 2 个表
- json - 在 Angular 9 中为 JSON 创建模型