Get Power Platform Licenses using PowerShell
Clear-Host
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell
Add-PowerAppsAccount
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$OutputFilePath = "$scriptPath\Licenses.csv"
$TimeoutInMinutes = 2
$ApiVersion = "2016-11-01"
$getLicensesUri = "https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/scopes/admin/exportServicePlans?api-version={apiVersion}"
$getLicensesResponse = InvokeApi -Method POST -Route $getLicensesUri -ApiVersion $ApiVersion -Verbose $false
if ($getLicensesResponse.StatusCode -eq 202)
{
$getLicensesUri = $getLicensesResponse.Headers['Location']
$currentTime = Get-Date -format HH:mm:ss
$nextTime = Get-Date -format HH:mm:ss
$TimeDiff = New-TimeSpan $currentTime $nextTime
#Wait until the operation complete, there is an error, or we hit a timeout
while((-not [string]::IsNullOrEmpty($getLicensesUri)) -and ($getLicensesResponse.StatusCode -eq 202) -and ($TimeDiff.TotalMinutes -lt $TimeoutInMinutes))
{
Start-Sleep -s 5
$getLicensesResponse = InvokeApiNoParseContent -Route $getLicensesUri -Method GET -ApiVersion $ApiVersion -Verbose: $false
$nextTime = Get-Date -format HH:mm:ss
$TimeDiff = New-TimeSpan $currentTime $nextTime
}
if ($TimeDiff.TotalMinutes -ge $TimeoutInMinutes)
{
Write-Error "Get-AdminPowerAppLicenses timeout."
throw
}
}
if ($getLicensesResponse.Content -ne $null)
{
$jobject = ConvertFrom-Json($getLicensesResponse.Content)
$csvFileUri = $jobject.sharedAccessSignature
Invoke-WebRequest $csvFileUri -OutFile $OutputFilePath
}
No comments:
Post a Comment