using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.Consumption;
using Microsoft.Azure.Management.Consumption.Models;
using Microsoft.Rest.Azure;
class Program
{
static async Task Main(string[] args)
{
string tenantId = "xxxxxx5";
string clientId = "xxxxxx35cb";
string clientSecret = "xxxxxx8dkdBH";
string subscriptionId = "xxxxxxb2a7";
var serviceClientCredentials = ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret).Result;
var consumptionManagementClient = new ConsumptionManagementClient(serviceClientCredentials)
{
SubscriptionId = subscriptionId
};
// Get the first and last day of the current month
var firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
// Create a filter to get the usage details for the current month
var filter = $"properties/usageStart ge '{firstDayOfMonth}' and properties/usageEnd le '{lastDayOfMonth}'";
// Get the usage details for the current month
var dailyCosts = new List<UsageDetail>();
IPage<UsageDetail> usageDetails = await consumptionManagementClient.UsageDetails.ListAsync(filter: filter);
dailyCosts.AddRange(usageDetails);
foreach (var usageDetail in dailyCosts)
{
Console.WriteLine($"{usageDetail.UsageStart}: {usageDetail.PretaxCost}");
}
Console.WriteLine(dailyCosts.Sum(x => x.PretaxCost));
Console.ReadLine();
Console.WriteLine(
$"Total cost for the current month: {dailyCosts.Sum(x => x.PretaxCost)}");
Console.ReadLine();
1条答案
按热度按时间pkbketx91#
字符串
输出:-
x1c 0d1x的数据