Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PowerShell获取azure订阅的当前成本_Azure_Powershell_Azure Powershell_Subscription - Fatal编程技术网

如何使用PowerShell获取azure订阅的当前成本

如何使用PowerShell获取azure订阅的当前成本,azure,powershell,azure-powershell,subscription,Azure,Powershell,Azure Powershell,Subscription,我正在尝试使用PowerShell获取azure订阅的当前成本。 期望输出: e、 g: 货币=英镑 当前成本=370.74 Get-AzConsumptionUsageDetail -BillingPeriodName 202105 但这并没有提供所需的输出。请尝试以下powershell脚本: $tenantId="76a1f773...b-86b9-d1ced3e15cda" $clientId="0159ec7d-f...-a680-c4d40ab7a3

我正在尝试使用PowerShell获取azure订阅的当前成本。

期望输出:

e、 g:

货币=英镑

当前成本=370.74

Get-AzConsumptionUsageDetail -BillingPeriodName 202105

但这并没有提供所需的输出。

请尝试以下powershell脚本:

$tenantId="76a1f773...b-86b9-d1ced3e15cda"
$clientId="0159ec7d-f...-a680-c4d40ab7a36c"
$clientSecret="o4eq4jj...I26uz26W~"
$secSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force

$pscredential = New-Object System.Management.Automation.PSCredential ($clientId, $secSecret)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

$dexResourceUrl="https://management.azure.com/"
$context = Get-AzContext
$token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $dexResourceUrl).AccessToken


$SubscriptionId = '3465e081-85b6-4b54-a3e1-15675acb615f'
$billingperiod = '202010-1'

#Create the REST-URL
$usageURL ="https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"

$header = @{
    'Authorization' = "Bearer $($token)"
    "Content-Type" = "application/json"
}
 
$UsageData = Invoke-RestMethod `
    -Method Get `
    -Uri $usageURL `
    -ContentType application/json `
    -Headers $header 

ConvertTo-Json $UsageData

来源:

你能看看这是否适合你吗:

  • 请尝试
    AzConsumptionUsageDetail-BillingPeriodName |测量对象税前成本-Sum
    。其中期间名称的格式为
    20210601
  • 编辑:意识到我错过了你帖子中显而易见的内容,你已经尝试过了——我将把它留在这里,以防其他人仍然发现它对其他人有帮助

  • 否则,请为所有订阅设置预算,然后使用
    Get-AzConsumptionBudget

  • 在OP的帮助下,我们能够更接近他所需要的东西。

    下面的查询将为您提供所述月份的成本。e、 g:五月份的费用

    Get-AzConsumptionUsageDetail -BillingPeriodName 202105
    
    但如果您的订阅不是从5月1日开始,那么在这种情况下,上述查询将为您提供错误的成本。e、 g:您的计费周期是从5月10日到6月9日

    要了解准确计费周期的成本,您需要首先获取当前计费周期,然后根据计费周期开始/结束日期获取成本

    $currentBillingPeriod = Get-AzBillingPeriod -MaxCount 1
    $startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("dd-MM-yyyy")
    Write-Host "currentBillingPeriod startDate : " $startDate
    $endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("dd-MM-yyyy")
    Write-Host "currentBillingPeriod endDate : " $endDate
    
    $currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum
    Write-Host "Current Cost of Subscription : " $currentCost.Sum
    

    这回答了你的问题吗?不,这个输出非常复杂。我只想要订阅的当前总成本。谢谢回复。这给了我资源方面的数据。不完全是我想要的。我不认为微软公开了任何API或命令可以直接给出订阅的成本。是的,我有同样的感觉。你最好查询剩下的部分,因为我错过了“度量对象税前成本-总和”。它给了我我想要的当前成本。虽然它没有给我货币类型的信息。e、 g:英镑或印度卢比。你知道这是否也是可能的吗?否则我会从我当前的查询结果中得到它,在那里我有详细的输出,包括货币。没有问题。不幸的是,我找不到获取货币的方法,但正如您所说,从结果中获取货币会更容易-如果您不介意的话,请在获得正确的过滤器/格式输出查询$consumption=get-AzConsumptionUsageDetail-BillingPeriodName$billingPeriod-Top 1写入主机后,与我们分享如何获取货币“订阅货币:$consumption.Currency