Public/Curanet/M365/Get-CuranetM365AzureBilling.ps1

function Get-CuranetM365AzureBilling() {
    Param(
        [Parameter(Mandatory)]
        [string]$SubscriptionId,
        [Parameter(Mandatory)]
        [ValidateSet("3370", "3850")]
        [string]$Account,
        [Parameter(Mandatory)]
        [DateTime]$StartDate,
        [Parameter(Mandatory)]
        [DateTime]$EndDate
    )

    if( ($null -eq (Get-Variable -Name "$($Account)AccessTokenExpire" -Scope Global -ValueOnly -ErrorAction SilentlyContinue)) -or ((Get-Date) -gt (Get-Variable -Name "$($Account)AccessTokenExpire" -Scope Global -ValueOnly -ErrorAction SilentlyContinue)) ) {
        Get-CuranetAccessToken -Account $Account
    }

    $AccessToken = (Get-Variable -Name "$($Account)AccessToken" -Scope Global -ValueOnly -ErrorAction SilentlyContinue).access_token

    $AuthorizationHeader = @{
        Authorization="Bearer $AccessToken"
    }

    Write-Host "Retreiving Azure Billing from Curanet $($Account) API for SubscriptionId: $($SubscriptionId) StartDate: $($submitStartDate) EndDate: $($submitEndDate)"

    $Body = @{
        startDate = $submitStartDate
        endDate = $submitEndDate
        costType = "Usage"
        timeFrame = "Custom"
    } | ConvertTo-Json

    try {
        $AzureBilling = (Invoke-WebRequest -Uri "https://api.curanet.dk/microsoft365/v1/Subscriptions/$($SubscriptionId)/Azurebilling/Query" -Headers $AuthorizationHeader -UseBasicParsing -Method POST -Body $Body -ContentType "application/json").Content | ConvertFrom-Json
    } catch {
        throw "Failed to retreive Azure Billing from Curanet $($Account) API: $_"
    }

    return $AzureBilling
}