Power Platform tenant reporting with CLI for Microsoft 365

Power Platform tenant reporting with CLI for Microsoft 365 header image

Ever had to do a estimate on how large an environment is? How much impact certain settings would have on an organization? Over the last few months I have been working with several customers running Power Platform in their organization. And the first thing we often do is an implementation of the Center of Excellence. But in a lot of cases there is this pre-sales stage where we can’t really install anything but still want to get a feeling how large of an environment we are taking on. So after a quiet summer things with some time off I finally got back to CLI for Microsoft 365 ☀️.

Insights

The CLI for Microsoft 365 already has a load of samples available, but for this case there wasn’t one yet. So to get started the script needs to do a few things:

  1. Retrieve all environments
  2. Retrieve all flows and apps per environment
  3. Save the details we want to report on (we need an additional check to retrieve the user as the Power Platform API’s are not providing those by default)

By creating a pscustomobject we can store the details we want and save everything to a CSV file. When you start thinking about these steps the commands can be found in the documentation:

  1. Get all environments m365 pp environment list --asAdmin
  2. Get the apps m365 pa app list --environment $envId --asAdmin
  3. Get the flows m365 flow list --environment $envId --asAdmin

This does imply that you are an admin, so running the commands without Power Platform administrator account will return either a access denied or only the stuff you are explicitly added as an owner to. Finally once everything has been executed you can read the CSV in an editor of your choice.

Power Platform report results in Excel

CLI for Microsoft 365 sample to report all environments

$m365Status = m365 status

if ($m365Status -eq "Logged Out") {
    # Connection to Microsoft 365
    m365 login
}

$environments = m365 pp environment list --asAdmin | ConvertFrom-Json

Write-Host -f Green "Processing $($environments.Count) environments";

$results = @()

$environments | ForEach-Object {
  Write-Host -f Green "Processing environment: $($_.displayName)"
  $envId = $($_.name)

  $results += [pscustomobject]@{
    type        = "environment"
    id          = $envId
    displayName = $_.displayName
    envId       = $envId
    lifeCycleId = $_.properties.environmentSku
    status      = $_.properties.isDefault ? "Default" : "NotDefault"
    createdByUpn = $_.properties.createdBy.userPrincipalName ? $_.properties.createdBy.userPrincipalName : ""
  }

  $apps = m365 pa app list --environment $envId --asAdmin | ConvertFrom-Json
  Write-Host -f Green "Processing: $($apps.Count) apps"

  $apps | ForEach-Object {

    $results += [pscustomobject]@{
      type              = $_.appType
      id                = $_.name
      displayName       = $_.displayName
      envId             = $envId
      lifeCycleId       = $_.properties.lifeCycleId
      status            = $_.properties.status
      createdByUpn      = $_.properties.createdBy.userPrincipalName
    }
  }

  $flows = m365 flow list --environment $envId --asAdmin | ConvertFrom-Json
  Write-Host -f Green "Processing: $($flows.Count) flows"

  $flows | ForEach-Object {
    $createUser = m365 aad user get --id $_.properties.creator.userId | ConvertFrom-Json

    $results += [pscustomobject]@{
      type          = "flow"
      id            = $_.name
      displayName   = $_.displayName
      envId         = $envId
      lifeCycleId   = $_.properties.lifeCycleId
      status        = $_.properties.state
      createdByUpn  = $createUser.userPrincipalName
    }
  }
}

$results | Export-Csv -Path .\cli-powerplatform-report.csv -NoTypeInformation

Write-Host -f Green "Done"
Loading comments…