Power BI API Basics with PowerShell
This article will cover using the PowerShell MicrosoftPowerBIMgmt module to run a report, it calls the underlying APIs without rest calls. I will cover using direct REST API calls in another article.
Let's start with a simple sample that will get workspaces and the underlying PBI reports.
Prerequisites:
Goals:
Create a PowerShell script that will retrieve workspaces you have access to and list out the reports contained.
# DISCLAIMER:
# PowerShell code is provided for educational purposes only.
# It is not intended for production use.
# There is no warranty or liability for any issues or damages that may arise from using this script.
# Use at your own risk.
# Tested on PowerShell 7.4.6 x64
#1 Save module with version to your local machine with PowerShell, you only do this step once.
# If you want to keep modules local separated by versions
Save-Module MicrosoftPowerBIMgmt -Path 'C:\PSModules\'
# If you want easy global install, is you use this disregard the imports below, just use Import-Module MicrosoftPowerBIMgmt
# Install-Module -Name MicrosoftPowerBIMgmt -AllowClobber -Force
#2 Load the module
# First two modules must load in order
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Profile\1.2.1111\MicrosoftPowerBIMgmt.Profile.psd1
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Admin\1.2.1111\MicrosoftPowerBIMgmt.Admin.psd1
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Workspaces\1.2.1111\MicrosoftPowerBIMgmt.Workspaces.psd1
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Capacities\1.2.1111\MicrosoftPowerBIMgmt.Capacities.psd1
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Data\1.2.1111\MicrosoftPowerBIMgmt.Data.psd1
Import-Module C:\PSModules\MicrosoftPowerBIMgmt.Reports\1.2.1111\MicrosoftPowerBIMgmt.Reports.psd1
Import-Module 'C:\PSModules\MicrosoftPowerBIMgmt\1.2.1111\MicrosoftPowerBIMgmt.psd1'
#3 Connect with your credentials, you may be prompted to log in with a pop-up.
领英推荐
Connect-PowerBIServiceAccount
#4 Create a collection to hold our list of reports
$reportCollection = New-Object System.Collections.ArrayList
#5 Get all PBI workspaces that you have at least Read permission within
$allOwnedWorkspaces = Get-PowerBIWorkspace -All
#6 Loop the workspaces and pull the reports and add them to your collection
foreach ($workspace in $allOwnedWorkspaces) {
$reports = Get-PowerBIReport -WorkspaceId $workspace.Id
foreach ($report in $reports) {
Write-Host $report.Name -ForegroundColor Yellow
$null = $reportCollection.Add([PSCustomObject]@{
WorkspaceName = $workspace.Name
ReportName = $report.Name
ReportId = $report.Id
ReportWebUrl = $report.WebUrl
})
}
}
#7 Export the collection to a .CSV file on your local machine.
$reportCollection | Export-Csv -Path 'C:\Reports\PowerBIReports.csv' -NoTypeInformation
I will make an article about using REST to access the API next. It will be a bit more involved since it will need an application principle.