Automating Power BI Report Deployment Using PowerShell

Loading

Deploying Power BI reports manually can be time-consuming. PowerShell allows you to automate publishing, updating, and managing reports efficiently. This guide will walk you through automating Power BI report deployment using PowerShell and Power BI REST API.


Step 1: Prerequisites

1. Required Permissions

  • You need Power BI Service Administrator or Global Administrator permissions.
  • You must have a Power BI Pro or Premium Per User (PPU) license.

2. Install Required PowerShell Modules

Install Power BI Module

Install-Module MicrosoftPowerBIMgmt -Scope CurrentUser -Force

Import Power BI Module

Import-Module MicrosoftPowerBIMgmt

Now, you have Power BI PowerShell module installed!


Step 2: Authenticate to Power BI

You need to sign in before deploying reports.

Connect-PowerBIServiceAccount

This will prompt you to log in to your Microsoft Power BI account.

Now, you are authenticated!


Step 3: Get Workspace Details

You need to retrieve the Workspace ID where the report will be published.

# Get all workspaces
Get-PowerBIWorkspace -Scope Organization | Select-Object Id, Name

Example Output

Id                                   Name
------------------------------------ -----------------------------------
abcd1234-5678-90ef-ghij-klmnopqrstuv Sales Reports
efgh5678-1234-ijkl-mnop-qrstuvwxyzab Marketing Analytics

Now, you have the Workspace ID!


Step 4: Upload a Power BI Report (.PBIX) to a Workspace

Use the New-PowerBIReport command to publish a report.

# Define variables
$workspaceId = "abcd1234-5678-90ef-ghij-klmnopqrstuv"
$pbixFilePath = "C:\Reports\SalesReport.pbix"

# Upload the report
New-PowerBIReport -Path $pbixFilePath -WorkspaceId $workspaceId -Name "Sales Report"

Report uploaded successfully!


Step 5: Update an Existing Power BI Report

If a report already exists, you can overwrite it.

# Overwrite existing report
New-PowerBIReport -Path $pbixFilePath -WorkspaceId $workspaceId -Name "Sales Report" -ConflictAction CreateOrOverwrite

Existing report updated!


Step 6: Assign Report Permissions

To grant access to users:

# Assign permissions to a user
$workspaceId = "abcd1234-5678-90ef-ghij-klmnopqrstuv"
$userEmail = "user@example.com"

Add-PowerBIWorkspaceUser -Id $workspaceId -UserPrincipalName $userEmail -AccessRight Viewer

User access assigned!


Step 7: Refresh Power BI Dataset After Deployment

After uploading a report, refresh the dataset to ensure it uses the latest data.

# Get Dataset ID
$datasetId = (Get-PowerBIDataset -WorkspaceId $workspaceId | Where-Object { $_.Name -eq "Sales Report" }).Id

# Trigger dataset refresh
Invoke-PowerBIRestMethod -Url "groups/$workspaceId/datasets/$datasetId/refreshes" -Method Post

Dataset refresh triggered!


Step 8: Automate Deployment Using a Scheduled Task

To fully automate deployment, create a PowerShell script and run it using Task Scheduler.

1. Save Script as a .PS1 File

Save the following as DeployPowerBI.ps1:

# Connect to Power BI
Connect-PowerBIServiceAccount

# Define Variables
$workspaceId = "abcd1234-5678-90ef-ghij-klmnopqrstuv"
$pbixFilePath = "C:\Reports\SalesReport.pbix"

# Publish Report
New-PowerBIReport -Path $pbixFilePath -WorkspaceId $workspaceId -Name "Sales Report" -ConflictAction CreateOrOverwrite

# Refresh Dataset
$datasetId = (Get-PowerBIDataset -WorkspaceId $workspaceId | Where-Object { $_.Name -eq "Sales Report" }).Id
Invoke-PowerBIRestMethod -Url "groups/$workspaceId/datasets/$datasetId/refreshes" -Method Post

2. Schedule the Task

  1. Open Task Scheduler in Windows.
  2. Click Create Basic Task > Name: “Power BI Report Deployment”.
  3. Click Next and select Daily (or any preferred frequency).
  4. Choose Start a Program > Browse to powershell.exe.
  5. In Arguments, add: -ExecutionPolicy Bypass -File "C:\Scripts\DeployPowerBI.ps1"
  6. Click Finish.

Now, your Power BI reports will be automatically deployed on schedule!

Leave a Reply

Your email address will not be published. Required fields are marked *