无法使用 Azure Runbook 获取当前订阅成本
Unable to get current cost of subscription using Azure Runbook
我正在尝试使用 azure 运行book 获取订阅当前费用,但显示 'Bad Request' 错误。相同的脚本在我的本地机器上运行良好。
仅供参考:我更新了 Runbook 库中的 Az.Accounts 和 Az.Billing 模块。
Powershell:
Write-Host "Get the subscription current billing period"
$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
Write-Host "Get the subscription current cost"
$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum
Write-Host "Current Cost of Subscription : " $currentCost.Sum
本地 powershell 的输出:
Get the subscription current billing period
currentBillingPeriod startDate : 11-08-2021
currentBillingPeriod endDate : 10-09-2021
Get the subscription current cost
Current Cost of Subscription : 497.729683916108
在 Azure Runbook 上 运行 时的输出:
Get-AzConsumptionUsageDetail : Operation returned an invalid status code 'BadRequest' At line:44 char:16 + ... rrentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDa ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzConsumptionUsageDetail], ErrorResponseException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Consumption.Cmdlets.UsageDetails.GetAzureRmConsumptionUsageDetail
调试输出:
Body:
{
"error": {
"code": "400",
"message": "Invalid time range, start: 2021-11-08 end: 2021-10-09 (Request ID: dc5f1cc4-4361-4378-adfd-0b9065ecbce3)"
}
}
Get-AzConsumptionUsageDetail : A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Operation returned an invalid status code 'BadRequest' At line:44 char:16 + ... rrentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDa ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [Get-AzConsumptionUsageDetail], HostException + FullyQualifiedErrorId : HostFunctionNotImplemented,Microsoft.Azure.Commands.Consumption.Cmdlets.UsageDetails.GetAzureRmConsumptionUsageDetail
终于可以解决这个问题了。实际上,我在 ("dd-MM-yyyy") 中为 'BillingPeriodStartDate' 和 'BillingPeriodEndDate' 传递日期格式。
日期格式应为 ("yyyy-MM-dd")
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("yyyy-MM-dd")
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("yyyy-MM-dd")
我不知道为什么它在我的本地机器上可以正确使用 ("dd-MM-yyyy") 格式。使用 ("yyyy-MM-dd") 格式后,它在本地和 Azure runbook 上工作。
调试日志确实帮助我找出了确切的问题。
我正在尝试使用 azure 运行book 获取订阅当前费用,但显示 'Bad Request' 错误。相同的脚本在我的本地机器上运行良好。
仅供参考:我更新了 Runbook 库中的 Az.Accounts 和 Az.Billing 模块。
Powershell:
Write-Host "Get the subscription current billing period"
$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
Write-Host "Get the subscription current cost"
$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum
Write-Host "Current Cost of Subscription : " $currentCost.Sum
本地 powershell 的输出:
Get the subscription current billing period
currentBillingPeriod startDate : 11-08-2021
currentBillingPeriod endDate : 10-09-2021
Get the subscription current cost
Current Cost of Subscription : 497.729683916108
在 Azure Runbook 上 运行 时的输出:
Get-AzConsumptionUsageDetail : Operation returned an invalid status code 'BadRequest' At line:44 char:16 + ... rrentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDa ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzConsumptionUsageDetail], ErrorResponseException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Consumption.Cmdlets.UsageDetails.GetAzureRmConsumptionUsageDetail
调试输出:
Body:
{
"error": {
"code": "400",
"message": "Invalid time range, start: 2021-11-08 end: 2021-10-09 (Request ID: dc5f1cc4-4361-4378-adfd-0b9065ecbce3)"
}
}
Get-AzConsumptionUsageDetail : A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Operation returned an invalid status code 'BadRequest' At line:44 char:16 + ... rrentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDa ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [Get-AzConsumptionUsageDetail], HostException + FullyQualifiedErrorId : HostFunctionNotImplemented,Microsoft.Azure.Commands.Consumption.Cmdlets.UsageDetails.GetAzureRmConsumptionUsageDetail
终于可以解决这个问题了。实际上,我在 ("dd-MM-yyyy") 中为 'BillingPeriodStartDate' 和 'BillingPeriodEndDate' 传递日期格式。 日期格式应为 ("yyyy-MM-dd")
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("yyyy-MM-dd")
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("yyyy-MM-dd")
我不知道为什么它在我的本地机器上可以正确使用 ("dd-MM-yyyy") 格式。使用 ("yyyy-MM-dd") 格式后,它在本地和 Azure runbook 上工作。
调试日志确实帮助我找出了确切的问题。