At this point you may be starting to wonder how many PowerShell scripts I have for working with PowerBI, turns out quite a few! Uploading new reports with PowerShell is the latest one!
As Prism grew I was looking at more workspaces and more reports, and so I turned to what I knew best at the time, PowerShell. Whilst going into PBI desktop, ensuring the data was loaded up then publishing to the PowerBI service doesn’t take a huge amount of time individually, but when building a product I needed much greater flexibility.
So what this latest script does is solve that exact problem.
The PBIX files I have are all loaded into source control but have a copy ready that is synced via VsCode. This script will pick up that file and upload it to the PowerBI workspace as required. (Even creating the PowerBI workspace and adding permissions at the same time if needed!)
In my case and something you may be interested in as well, I then combined this script with a couple of the others I have already posted about. Adding parameters and setting refresh times. At this point you can automate the whole thing end to end!
How does it work?
This script whilst looks simple, will do quite a few things!
Firstly, you need to provide a name for your workspace and a name for your report. These are going to be what the script uses, regardless of what your PBIX file is named already.
If using this part of the code, you will also need to supply your capacity ID. This is so the workspace can then link up with your premium / embedded / fabric etc capacity. Note this will require you to have a level of admin permissions in your organisation if you want to do this automated.
Once it has set those, it will add a new user (usually best to have more than just yourself as an admin on these things). In this example ive added a user, you can add groups , distribution lists all sorts. Prism for example, we have support teams as group who get applied to new workspaces.
Lastly it will upload the PBIX file you specified, name it how you wanted all ready to go!
The Script
Connects to the PowerBI service Account
Uses the parameters you have set to:
Create a PowerBI workspace
Add it to the specified capacity
Adds a new admin user
Add the PowerBI report
Like I say, combine this with the other PowerShell scripts I have posted about uploading new reports with PowerShell will then go even further! Especially if you are using PowerBI as part of a product or simply dealing with many workspaces and reports.
https://github.com/AetherAdv/powerbi_powershell_addnew
# Ensure Power BI module is installed
Import-Module MicrosoftPowerBIMgmt
# Connect to Power BI (if not already connected)
Connect-PowerBIServiceAccount
# Define conflict action
$Conflict = "CreateOrOverwrite"
# Define Capacity ID
$CapacityId = "00000000-0000-0000-0000-000000000000" # Replace with your capacity ID
# Define workspace details
$WorkspaceName = "AETHER - MyNewWorkspace"
# Define report details
$ReportName = "AETHER - MyReport"
# Define PBIX file path
$DeployPath = "C:\Temp\myreport.pbix"
# Create the workspace
$Workspace = New-PowerBIWorkspace -Name $WorkspaceName
# Wait a few seconds for workspace creation - just incase it needs time to be accessible
Start-Sleep -Seconds 5
# Assign the workspace to a Power BI Premium capacity - Not this will require some additional licence and access considerations to do with commands!
Set-PowerBIWorkspace -Id $Workspace.Id -CapacityId $CapacityId -Scope Organization
# Add a user as an admin
Add-PowerBIWorkspaceUser -Id $Workspace.Id -UserPrincipalName "[email protected]" -AccessRight Admin
# Publish the Power BI report
New-PowerBIReport -Path $DeployPath -Name $ReportName -WorkspaceId $Workspace.Id -ConflictAction $Conflict