Skip to content

Commit 0637071

Browse files
committed
feat: add -UseInteractiveAuth switch for interactive login in Get-PBIWorkspaceUsageReport.ps1
1 parent 90afafc commit 0637071

2 files changed

Lines changed: 273 additions & 68 deletions

File tree

docs/wiki/Graph Commands/Get-PBIWorkspaceUsageReport.md

Lines changed: 105 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
`Get-PBIWorkspaceUsageReport.ps1` generates a comprehensive Power BI workspace and report usage report. It inventories all reports across all workspaces (including personal workspaces), correlates them with Power BI Activity Log data for up to 90 days, and produces a detailed usage analysis showing view counts and unique users per report. Outputs in CSV or JSON format.
5+
`Get-PBIWorkspaceUsageReport.ps1` generates a comprehensive Power BI workspace and report usage report. It inventories all reports across all workspaces (including personal workspaces), correlates them with Power BI Activity Log data for the last 30 days by default (up to 90 days for Fabric/Premium capacities), and produces a detailed usage analysis showing view counts and unique users per report. Supports both Service Principal and interactive user authentication. Outputs in CSV or JSON format.
66

77
## Features
88

@@ -13,27 +13,62 @@
1313
- **Stale Report Identification** - Reports with zero views are surfaced for potential cleanup
1414
- **CSV and JSON Export** - Choose output format; JSON uses proper arrays for user lists
1515
- **Pre-Flight Validation** - Validates PowerShell version, output directory, and write permissions before starting
16-
- **Service Principal Auth** - Uses app registration with Power BI Admin APIs
16+
- **Dual Authentication Modes** - Service Principal (client credentials) for automated/pipeline use, or interactive browser login via `-UseInteractiveAuth` for ad-hoc runs
1717

1818
## Prerequisites
1919

2020
### PowerShell Version
2121
- **PowerShell 5.1 or later** (validated at runtime)
2222

2323
### Required Permissions
24-
The service principal must be authorized for Power BI Admin APIs. One of:
25-
- `Tenant.Read.All` permission in Power BI Service
26-
- `Tenant.ReadWrite.All` permission in Power BI Service
2724

28-
**AND** one of the following tenant-level configurations:
29-
- Service principal added to the **Power BI Service Admins** security group
30-
- Tenant setting **"Allow service principals to use Power BI admin APIs"** enabled in Power BI Admin Portal
25+
The script calls Power BI `/admin/` endpoints. Three things must all be in place:
3126

32-
### Azure App Registration Setup
33-
1. Register an App in Azure AD / Entra ID
27+
#### 1. Entra App Registration Permissions
28+
Add the following API permissions to the app registration in [Entra ID](https://entra.microsoft.com) and grant admin consent:
29+
30+
| API | Permission | Type |
31+
|-----|-----------|------|
32+
| Power BI Service | `Tenant.Read.All` | Application |
33+
34+
#### 2. Fabric Admin RBAC Role (required to access Admin Portal settings)
35+
The person configuring the Fabric tenant settings must hold the **Fabric Administrator** (or Global Administrator) role in Entra ID.
36+
37+
> Without this role, the [Fabric Admin Portal](https://app.powerbi.com/admin-portal/tenantSettings) tenant settings will not be accessible or editable.
38+
39+
To assign the role:
40+
**Entra ID → Roles and administrators → Fabric Administrator → Add assignment**
41+
42+
#### 3. Fabric Admin Portal — Admin API Settings
43+
Once you have the Fabric Administrator role, navigate to:
44+
45+
**[https://app.powerbi.com/admin-portal/tenantSettings](https://app.powerbi.com/admin-portal/tenantSettings)**
46+
47+
Scroll to the **Admin API settings** section and configure:
48+
49+
- **Setting:** "Service principals can access read-only admin APIs"
50+
- **Toggle:** Enabled
51+
- **Apply to:** Specific security groups
52+
- **Action:** Add the security group that contains your app registration's service principal
53+
54+
> ⚠️ **Common confusion:** "Service principals can call Fabric public APIs" (under Developer settings) is a **different setting** that covers regular endpoints only. The `/admin/` endpoints used by this script require the **Admin API settings** toggle above.
55+
56+
### Azure App Registration Setup (Service Principal auth)
57+
1. Register an App in Entra ID
3458
2. Create a client secret
35-
3. Add the service principal to the Power BI Service Admin group OR enable the tenant setting
36-
4. No Graph API permissions needed — only Power BI REST API access
59+
3. Grant `Tenant.Read.All` (Power BI Service) with admin consent
60+
4. Add the service principal to the security group allowed in the Admin API settings above
61+
5. No Microsoft Graph API permissions are needed — only Power BI REST API access
62+
63+
### Interactive Authentication (`-UseInteractiveAuth`)
64+
Use this for ad-hoc runs or while Service Principal permissions are still propagating.
65+
66+
**Requirements:**
67+
- The `MicrosoftPowerBIMgmt` PowerShell module: `Install-Module MicrosoftPowerBIMgmt -Scope CurrentUser`
68+
- Sign in with a **Power BI Administrator** or **Fabric Administrator** account
69+
- `TenantId` is still required; `ClientId` and `ClientSecret` are not used
70+
71+
> ℹ️ The same Admin API settings (above) apply — the signed-in user account must have Power BI Admin access to the tenant.
3772
3873
### Output Directory
3974
- Write permissions to the `OutputPath` directory (validated at runtime)
@@ -47,13 +82,18 @@ The service principal must be authorized for Power BI Admin APIs. One of:
4782

4883
### ClientId
4984
**Type:** String
50-
**Required:** Yes
51-
**Description:** App Registration (Service Principal) Client ID.
85+
**Required:** Yes *(Service Principal auth only)*
86+
**Description:** App Registration (Service Principal) Client ID. Not used when `-UseInteractiveAuth` is specified.
5287

5388
### ClientSecret
5489
**Type:** String
55-
**Required:** Yes
56-
**Description:** App Registration Client Secret. For production use, retrieve from Azure Key Vault rather than hardcoding.
90+
**Required:** Yes *(Service Principal auth only)*
91+
**Description:** App Registration Client Secret. For production use, retrieve from Azure Key Vault rather than hardcoding. Not used when `-UseInteractiveAuth` is specified.
92+
93+
### UseInteractiveAuth
94+
**Type:** Switch
95+
**Required:** No
96+
**Description:** Use interactive browser login instead of a service principal client secret. Requires the `MicrosoftPowerBIMgmt` module. When specified, `ClientId` and `ClientSecret` are not needed. `TenantId` is still required.
5797

5898
### OutputPath
5999
**Type:** String
@@ -71,53 +111,52 @@ The service principal must be authorized for Power BI Admin APIs. One of:
71111
### ActivityDays
72112
**Type:** Integer
73113
**Required:** No
74-
**Default:** `90`
114+
**Default:** `30`
75115
**Valid Range:** 1–90
76-
**Description:** Number of days of Power BI activity history to retrieve. Maximum is 90 (Power BI API limitation).
116+
**Description:** Number of days of Power BI activity history to retrieve. Standard Power BI audit log retains **30 days**; tenants with Fabric or Premium capacity may retain up to 90 days. Requesting dates outside the tenant's retention window returns a 400 error (skipped automatically per day).
77117

78118
## Usage Examples
79119

80-
### Example 1: Basic Report (CSV, Current Directory)
120+
### Example 1: Interactive Auth (Recommended for Ad-Hoc Use)
81121
```powershell
82122
.\Get-PBIWorkspaceUsageReport.ps1 `
83123
-TenantId "12345678-1234-1234-1234-123456789012" `
84-
-ClientId "abcdefab-1234-1234-1234-abcdefabcdef" `
85-
-ClientSecret "your-client-secret"
124+
-UseInteractiveAuth `
125+
-OutputPath "C:\Reports\PBI" `
126+
-OutputFormat "json"
86127
```
128+
Opens a browser login prompt. Use a Power BI Administrator account. No app registration needed.
87129

88-
### Example 2: Custom Output Path and Format
130+
### Example 2: Service Principal (Automated / Pipeline Use)
89131
```powershell
90132
.\Get-PBIWorkspaceUsageReport.ps1 `
91133
-TenantId "12345678-1234-1234-1234-123456789012" `
92134
-ClientId "abcdefab-1234-1234-1234-abcdefabcdef" `
93-
-ClientSecret "your-client-secret" `
94-
-OutputPath "C:\Reports\PowerBI" `
95-
-OutputFormat "json"
135+
-ClientSecret "your-client-secret"
96136
```
97137

98-
### Example 3: 60-Day Lookback with CSV Export
138+
### Example 3: Extended Window (Fabric/Premium Tenants)
99139
```powershell
100140
.\Get-PBIWorkspaceUsageReport.ps1 `
101141
-TenantId "12345678-1234-1234-1234-123456789012" `
102-
-ClientId "abcdefab-1234-1234-1234-abcdefabcdef" `
103-
-ClientSecret "your-client-secret" `
142+
-UseInteractiveAuth `
104143
-OutputPath "C:\Reports\PowerBI" `
105-
-ActivityDays 60
144+
-ActivityDays 90
106145
```
146+
> ⚠️ Only use `-ActivityDays` values above 30 if the tenant has Fabric or Premium capacity with extended audit retention. Standard tenants will see per-day failures for dates beyond 30 days (skipped automatically).
107147
108148
### Example 4: Identify Stale Reports
109149
```powershell
110150
# Run report
111151
.\Get-PBIWorkspaceUsageReport.ps1 `
112152
-TenantId "12345678-1234-1234-1234-123456789012" `
113-
-ClientId "<client-id>" `
114-
-ClientSecret "<secret>" `
153+
-UseInteractiveAuth `
115154
-OutputPath "C:\Reports\PowerBI"
116155
117156
# Filter stale reports from the usage CSV
118-
$usage = Import-Csv "C:\Reports\PowerBI\PBI_Report_Usage_*.csv" | Select-Object -First 1
157+
$usage = Import-Csv "C:\Reports\PowerBI\PBI_Report_Usage_*.csv"
119158
$stale = $usage | Where-Object { [int]$_.TotalViews_90d -eq 0 }
120-
Write-Host "Stale reports (0 views): $($stale.Count)"
159+
Write-Host "Stale reports (0 views in last 30 days): $($stale.Count)"
121160
$stale | Select-Object ReportName, WorkspaceName, WorkspaceType | Format-Table
122161
```
123162

@@ -145,8 +184,11 @@ Per-report usage aggregation sorted by most-viewed.
145184
| ReportName | Display name of the report |
146185
| WorkspaceName | Workspace containing the report |
147186
| WorkspaceType | `Shared` or `Personal` |
148-
| TotalViews_90d | Total view events in the period |
187+
| TotalViews_90d | Total ViewReport events in the activity window |
149188
| UniqueUsers_90d | Count of distinct users who viewed |
189+
| UserList_90d | Semicolon-delimited list of user IDs (CSV) or JSON array (JSON format) |
190+
| DatasetId | Power BI Dataset GUID backing the report |
191+
| ReportWebUrl | Direct URL to open the report in Power BI Service |
150192

151193
### User Details (`PBI_Report_UserDetails_<timestamp>.<ext>`)
152194
Per-user, per-report view counts.
@@ -155,8 +197,11 @@ Per-user, per-report view counts.
155197
|--------|-------------|
156198
| ReportId | Power BI Report GUID |
157199
| ReportName | Display name of the report |
158-
| UserId | Azure AD User Object ID or UPN |
200+
| UserId | Azure AD User UPN or Object ID |
159201
| ViewCount_90d | Number of times this user viewed the report |
202+
| LastViewed | Timestamp of the user's most recent view event |
203+
| WorkspaceName | Workspace containing the report |
204+
| WorkspaceType | `Shared` or `Personal` |
160205

161206
### Summary Text (`PBI_Usage_Summary_<timestamp>.txt`)
162207
Human-readable summary including:
@@ -186,25 +231,39 @@ Human-readable summary including:
186231
## Common Issues & Troubleshooting
187232

188233
### Issue: "Unauthorized" / 401 on Power BI API
189-
**Causes:**
190-
- Service principal not added to Power BI Service Admin group
191-
- Tenant setting "Allow service principals to use Power BI admin APIs" not enabled
234+
**Root Cause:**
235+
The script calls `/admin/` endpoints. `Tenant.Read.All` being granted and the "Service principals can call Fabric public APIs" setting being enabled is **not sufficient** — the Admin API setting is controlled by a separate toggle.
236+
237+
**Resolution steps:**
238+
239+
**Step 1 — Verify you have Fabric Admin access to configure tenant settings:**
240+
The person making changes must hold the **Fabric Administrator** (or Global Administrator) role.
241+
> Entra ID → Roles and administrators → Fabric Administrator → Add assignment
242+
243+
**Step 2 — Enable Admin API access in the Fabric Admin Portal:**
244+
Navigate to: **[https://app.powerbi.com/admin-portal/tenantSettings](https://app.powerbi.com/admin-portal/tenantSettings)**
245+
246+
Scroll to **Admin API settings** and configure:
247+
- Enable: **"Service principals can access read-only admin APIs"**
248+
- Set **Apply to:** Specific security groups
249+
- Add the security group containing your app registration's service principal
192250

193-
**Solutions:**
194-
1. Add the service principal to the **Power BI Administrator** role in Azure AD
195-
2. Or, in Power BI Admin Portal → Tenant settings → Enable "Allow service principals to use Power BI admin APIs" and add the security group containing your SP
251+
> ⚠️ **Common confusion:** "Service principals can call Fabric public APIs" (Developer settings) covers regular/public endpoints only — it does **not** grant access to `/admin/` endpoints used by this script.
196252
197253
### Issue: No workspaces returned
198254
**Cause:** Insufficient Power BI admin permissions.
199255

200256
**Solution:** Verify the service principal has `Tenant.Read.All` in Power BI and admin API access is enabled.
201257

202-
### Issue: Activity log returns empty results
203-
**Cause:**
204-
- Activity logs only retain 90 days of data
258+
### Issue: Activity log returns empty results or per-day failures
259+
**Cause:**
260+
- Standard Power BI audit log retains only **30 days** of data. Requesting dates older than 30 days returns 400 errors (the script skips those days automatically and reports a count at the end)
205261
- Tenant may have activity logging disabled
206262

207-
**Solution:** Verify Power BI activity logging is enabled in the Power BI Admin Portal.
263+
**Solution:**
264+
- Use the default `ActivityDays 30` or explicitly pass `-ActivityDays 30` for standard tenants
265+
- Only use higher values (up to 90) if the tenant has **Fabric or Premium capacity** with extended retention
266+
- Verify Power BI activity logging is enabled in the Power BI Admin Portal
208267

209268
### Issue: Pre-flight validation fails (PowerShell version)
210269
**Cause:** Running on PowerShell 5.0 or earlier.
@@ -239,6 +298,7 @@ $PSVersionTable.PSVersion
239298

240299
- **v1.0** - Initial release - Core inventory and usage correlation
241300
- **v1.1** (2026-03-12) - Added pre-flight validation, improved error handling and authentication, Export-Data with verbose logging, and wrapped main auth in try/catch
301+
- **v1.2.0** (2026-03-20) - Added `-UseInteractiveAuth` switch for interactive login via `MicrosoftPowerBIMgmt` module; switched activity event collection to `Get-PowerBIActivityEvent` cmdlet with client-side `ViewReport` filtering; changed default `ActivityDays` from 90 to 30 to match standard Power BI audit retention
242302

243303
## See Also
244304

0 commit comments

Comments
 (0)