Last active
September 19, 2025 15:36
-
-
Save joerodgers/665925981c820cc47e8dd8e1c89ebec9 to your computer and use it in GitHub Desktop.
Reports agent Copilot Credit consumption per agent per environment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -modules "Microsoft.PowerApps.Administration.PowerShell" | |
| function Get-FirstDayOfMonthDate | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| ) | |
| $month = [DateTime]::Today.Month | |
| $day = 1 | |
| $year = [DateTime]::Today.Year | |
| return [DateTime]::Parse( "$month/$day/$year" ) | |
| } | |
| function Get-EnvironmentAgentMessageConsumptionReport | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory=$true)] | |
| [string] | |
| $EnvironmentId, | |
| [Parameter(Mandatory=$false)] | |
| [DateTime] | |
| $StartDate = (Get-FirstDayOfMonthDate), | |
| [Parameter(Mandatory=$false)] | |
| [DateTime] | |
| $EndDate = ([DateTime]::Today) | |
| ) | |
| begin | |
| { | |
| $accessToken = Get-JwtToken -Audience "https://licensing.powerplatform.microsoft.com" | |
| $tenantId = $global:currentSession.tenantId | |
| $headers = @{ Authorization = "Bearer $accessToken" } | |
| } | |
| process | |
| { | |
| $uri = 'https://licensing.powerplatform.microsoft.com/v0.1-alpha/tenants/{0}/entitlements/MCSMessages/environments/{1}/resources?fromDate={2}&toDate={3}' -f $tenantId, $EnvironmentId, $StartDate.ToString("MM-dd-yyyy"), $EndDate.ToString("MM-dd-yyyy") | |
| $response = Invoke-RestMethod -Method GET ` | |
| -Uri $uri ` | |
| -Headers $headers ` | |
| -ErrorAction Stop | |
| foreach( $resource in $response.value.resources ) | |
| { | |
| [PSCustomObject] @{ | |
| EnvironmentId = $EnvironmentId | |
| Name = $resource.metadata.ResourceName | |
| Product = $resource.metadata.ProductName | |
| Feature = $resource.metadata.FeatureName | |
| BilledCopilotCredits = $resource.consumed | |
| NonBilledCopilotCredits = $resource.metadata.NonBillableQuantity | |
| } | |
| } | |
| } | |
| end | |
| { | |
| } | |
| } | |
| # requires power platform admin role when prompted. This is kinda a hack b/c Add-PowerAppsAccount clear existing sessions when run, so you can't load 2+ into the current session w/ that cmdlet. | |
| $null = Get-JwtToken -Audience "https://licensing.powerplatform.microsoft.com" -ErrorAction Stop | |
| $null = Get-JwtToken -Audience "https://service.powerapps.com/" -ErrorAction Stop | |
| # start and end date for the reporting period | |
| $start = [DateTime]::Today.AddDays( -30 ) | |
| $end = [DateTime]::Today | |
| # get environemnts with dataverse | |
| $environments = Get-AdminPowerAppEnvironment | Where-Object -Property "CommonDataServiceDatabaseType" -eq "Common Data Service for Apps" | |
| # enumerate environments and fetch agent usage | |
| $consumption = foreach( $environment in $environments ) | |
| { | |
| Get-EnvironmentAgentMessageConsumptionReport -EnvironmentId $environment.EnvironmentName -StartDate $start -EndDate $end | Select-Object @{ Name="EnvironmentName"; Expression={ $environment.DisplayName}}, * | |
| } | |
| # export usage to csv | |
| $consumption | Export-Csv -Path "copilotstudiocreditconsumption-$($start.ToString('yyyy-MM-dd'))-$($end.ToString('yyyy-MM-dd'))-2.csv" -NoTypeInformation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -modules "Microsoft.PowerApps.Administration.PowerShell" | |
| function Get-TenantAgentMessageConsumption | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory=$false)] | |
| [DateTime] | |
| $StartDate = (Get-FirstDayOfMonthDate), | |
| [Parameter(Mandatory=$false)] | |
| [DateTime] | |
| $EndDate = ([DateTime]::Today) | |
| ) | |
| begin | |
| { | |
| $tenantId = $global:currentSession.tenantId | |
| $pageNumber= 0 | |
| $pageSize = 100 | |
| } | |
| process | |
| { | |
| do | |
| { | |
| $accessToken = Get-JwtToken -Audience "https://licensing.powerplatform.microsoft.com" | |
| $headers = @{ Authorization = "Bearer $accessToken" } | |
| $pageNumber++ | |
| # rest api behind https://admin.preview.powerplatform.microsoft.com/billing/licenses/agents/CopilotStudio | |
| $uri = 'https://licensing.powerplatform.microsoft.com/v2.0/tenants/{0}/entitlements/MCSMessages/resources?fromDate={1}&toDate={2}&pageNumber={3}&pageSize={4}&searchRequest=' -f $tenantId, $StartDate.ToString("yyyy-MM-dd"), $EndDate.ToString("yyyy-MM-dd"), $pageNumber, $pageSize | |
| $response = Invoke-RestMethod -Method GET ` | |
| -Uri $uri ` | |
| -Headers $headers ` | |
| -ErrorAction Stop | |
| foreach( $resource in $response.resources ) | |
| { | |
| [PSCustomObject] @{ | |
| EnvironmentId = $resource.environmentId | |
| Name = $resource.metadata.ResourceName | |
| BilledCopilotCredits = $resource.consumed | |
| NonBilledCopilotCredits = $resource.metadata.NonBillableQuantity | |
| AsOfDate = $resource.asOfDate | |
| } | |
| } | |
| } | |
| while( $response.resources.count -eq $pageSize ) | |
| } | |
| end | |
| { | |
| } | |
| } | |
| # requires power platform admin role when prompted. This is kinda a hack b/c Add-PowerAppsAccount clear existing sessions when run, so you can't load 2+ into the current session w/ that cmdlet. | |
| Add-PowerAppsAccount -Audience "https://licensing.powerplatform.microsoft.com" -ErrorAction Stop | |
| # start and end date for the reporting period | |
| $start = [DateTime]::Today.AddDays( -30 ) | |
| $end = [DateTime]::Today | |
| Get-TenantAgentMessageConsumption -StartDate $start -EndDate $end | FT -AutoSize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment