Last active
September 2, 2026 19:03
-
-
Save joerodgers/ae152d46bb03394bba0f5b26ff37dc64 to your computer and use it in GitHub Desktop.
Example script using the Upsert Resource Threshold Power Platform API for setting agent level credit consumption limits.
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
| #requries -modules "Az.Accounts" | |
| function Get-AccessToken | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory=$true)] | |
| [string] | |
| $ResourceUrl | |
| ) | |
| if (-not (Get-AzContext)) | |
| { | |
| Write-Host "No Azure context - launching sign-in..." -ForegroundColor Yellow | |
| Connect-AzAccount -WarningAction SilentlyContinue | Out-Null | |
| } | |
| $azAccessToken = Get-AzAccessToken -ResourceUrl $ResourceUrl -WarningAction SilentlyContinue | |
| if ($azAccessToken.Token -is [System.Security.SecureString]) | |
| { | |
| return [System.Net.NetworkCredential]::new('', $azAccessToken.Token).Password | |
| } | |
| return $azAccessToken.Token | |
| } | |
| function Set-AgentCreditConsumptionLimit | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory=$true)] | |
| [Guid] | |
| $ResourceId, | |
| [Parameter(Mandatory=$true)] | |
| [int] | |
| $CreditLimit, | |
| [Parameter(Mandatory=$true)] | |
| [string] | |
| $EnvironmentId, | |
| [Parameter(Mandatory=$true)] | |
| [bool] | |
| $StopIfOverCapacity, | |
| [Parameter(Mandatory=$true)] | |
| [bool] | |
| $NotifyIfOverCapacity, | |
| [Parameter(Mandatory=$true)] | |
| [int] | |
| $NotificationThreshold | |
| ) | |
| begin | |
| { | |
| $accessToken = Get-AccessToken -ResourceUrl "https://api.powerplatform.com/" | |
| $headers = @{ "Authorization" = "Bearer $accessToken"; "Content-Type" = "application/json"; "Accept" = "application/json" } | |
| $object = [PSCustomObject] @{ | |
| resourceId = $ResourceId | |
| entitlementId = "MCSMessages" | |
| environmentId = $EnvironmentId | |
| stopResource = $false | |
| limit = $CreditLimit | |
| stopIfOverCapacity = $StopIfOverCapacity | |
| notifyIfOverCapacity = $NotifyIfOverCapacity | |
| notificationThreshold = $NotificationThreshold | |
| resourceConsumption = 0 | |
| } | |
| $body = $object | ConvertTo-Json -Compress | |
| } | |
| process | |
| { | |
| $uri = 'https://api.powerplatform.com/licensing/environments/{0}/entitlements/MCSMessages/resources/{1}/threshold?api-version=2024-10-01' -f $EnvironmentId, $ResourceId | |
| $response = Invoke-RestMethod -Method PUT -Uri $uri -Body $body.ToString() -Headers $headers -ErrorAction Stop -Verbose:$false | |
| $response | |
| } | |
| end | |
| { | |
| } | |
| } | |
| function Get-AgentCreditConsumptionLimit | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory=$true)] | |
| [Guid] | |
| $ResourceId | |
| ) | |
| begin | |
| { | |
| $accessToken = Get-AccessToken -ResourceUrl "https://api.powerplatform.com/" | |
| $headers = @{ "Authorization" = "Bearer $accessToken"; "Content-Type" = "application/json"; "Accept" = "application/json" } | |
| } | |
| process | |
| { | |
| $uri = 'https://api.powerplatform.com/licensing/entitlements/MCSMessages/resourceThresholds?api-version=2024-10-01' | |
| $response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers -ErrorAction Stop -Verbose:$false | |
| $response | Where-Object -Property "resourceId" -eq $ResourceId | |
| } | |
| end | |
| { | |
| } | |
| } | |
| Set-AgentCreditConsumptionLimit -ResourceId "edbd9620-66a5-f111-aaac-000d3a5a565d" -CreditLimit "1000" -EnvironmentId "1e41500f-50d7-e866-9509-e258f804191e" -StopIfOverCapacity $true -NotifyIfOverCapacity $true -NotificationThreshold 80 | |
| Get-AgentCreditConsumptionLimit -ResourceId "edbd9620-66a5-f111-aaac-000d3a5a565d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment