Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PBIQueryous/58544f1bbb34e6cb09cab3c0321e2db6 to your computer and use it in GitHub Desktop.
Save PBIQueryous/58544f1bbb34e6cb09cab3c0321e2db6 to your computer and use it in GitHub Desktop.
# Install the MicrosoftPowerBIMgmt module required for managing Power BI resources.
# This module is installed for the current user only, to avoid system-wide changes.
Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser
# Authenticate to the Power BI Service.
# This step is necessary to access and manage Power BI resources.
Login-PowerBIServiceAccount
# Function to retrieve all Power BI workspaces.
# It fetches data from the Power BI API and handles pagination to get all workspaces.
Function Get-Workspaces {
# Initialize an array to hold details of each workspace.
$workspaces = @()
# API endpoint for fetching Power BI groups (workspaces).
$url = "https://api.powerbi.com/v1.0/myorg/groups"
Do {
# Perform a REST call to the Power BI API to get workspace details.
$response = Invoke-PowerBIRestMethod -Url $url -Method Get
# Convert the JSON response into a PowerShell object.
$result = $response | ConvertFrom-Json
# Append the retrieved workspaces to our array.
$workspaces += $result.value
# Check for the link to the next page of results (pagination).
$url = $result.'@odata.nextLink'
} While ($url) # Continue this loop until there are no more pages.
# Return the array of workspaces.
return $workspaces
}
# Function to get items (Reports, Dashboards, Datasets, Dataflows) in a specific workspace.
# It requires workspace ID and type of item as parameters.
Function Get-WorkspaceItems($workspaceId, $type) {
# API endpoint for fetching items within a workspace, based on the provided type.
$url = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/$type"
# Perform a REST call to get details of the specified items.
$response = Invoke-PowerBIRestMethod -Url $url -Method Get
# Convert the JSON response to a PowerShell object and return it.
$result = $response | ConvertFrom-Json
return $result.value
}
# Execute the function to fetch all workspaces.
$allWorkspaces = Get-Workspaces
# Prepare an array to hold data for export.
$dataForExport = @()
# Iterate through each workspace to collect item details.
foreach ($workspace in $allWorkspaces) {
# Fetch reports, dashboards, datasets, and dataflows for each workspace.
$reports = Get-WorkspaceItems -workspaceId $workspace.id -type "reports"
$dashboards = Get-WorkspaceItems -workspaceId $workspace.id -type "dashboards"
$datasets = Get-WorkspaceItems -workspaceId $workspace.id -type "datasets"
$dataflows = Get-WorkspaceItems -workspaceId $workspace.id -type "dataflows"
# Add each report to the export data array with relevant details.
foreach ($report in $reports) {
$dataForExport += New-Object PSObject -Property @{
WorkspaceId = $workspace.id
WorkspaceName = $workspace.name
ItemType = "Report"
ItemId = $report.id
ItemName = $report.name
}
}
# Do the same for dashboards.
foreach ($dashboard in $dashboards) {
$dataForExport += New-Object PSObject -Property @{
WorkspaceId = $workspace.id
WorkspaceName = $workspace.name
ItemType = "Dashboard"
ItemId = $dashboard.id
ItemName = $dashboard.displayName
}
}
# And for datasets.
foreach ($dataset in $datasets) {
$dataForExport += New-Object PSObject -Property @{
WorkspaceId = $workspace.id
WorkspaceName = $workspace.name
ItemType = "Dataset"
ItemId = $dataset.id
ItemName = $dataset.name
}
}
# Finally, for dataflows.
foreach ($dataflow in $dataflows) {
$dataForExport += New-Object PSObject -Property @{
WorkspaceId = $workspace.id
WorkspaceName = $workspace.name
ItemType = "Dataflow"
ItemId = $dataflow.id
ItemName = $dataflow.name
}
}
}
# Export the collected data to a CSV file.
# The file will be saved in the current directory with no type information in headers.
$dataForExport | Export-Csv -Path "PowerBI_Workspace_Items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment