├── README.md └── Scripts ├── Retrieving-all-users-for-all-workspaces.ps1 ├── Deleting-datasets.ps1 ├── Retrieving-all-users-for-all-workspaces-and-reports.ps1 ├── Storing-activity-logs.ps1 └── Retrieving-all-objects-from-workspaces.ps1 /README.md: -------------------------------------------------------------------------------- 1 | # Curated list of Powershell scripts for PowerBI 2 | 3 | Various assorted and curated list of Powershell Scripts for Power BI. 4 | Most of the time using these modules: 5 | 6 | * MicrosoftPowerBIMgmt 7 | * MicrosoftPowerBIMgmt.Admin 8 | * MicrosoftPowerBIMgmt.Capacities 9 | * MicrosoftPowerBIMgmt.Data 10 | * MicrosoftPowerBIMgmt.Profile 11 | * MicrosoftPowerBIMgmt.Reports 12 | * MicrosoftPowerBIMgmt.Workspaces 13 | 14 | 15 | Features 16 | ==== 17 | 18 | * Deleting datasets ([Blogpost](https://tomaztsql.wordpress.com/2022/08/18/deleting-power-bi-datasets-using-powershell/)) 19 | * Storing activity logs ([Blogpost](https://tomaztsql.wordpress.com/2022/08/24/longterm-storage-of-power-bi-activity-logs-and-statistics-using-powershell/)) 20 | * Retrieving list of users for all workspaces ([Blogpost](https://tomaztsql.wordpress.com/2022/09/03/retrieving-list-of-users-for-all-workspaces-in-your-powerbi-tenant-using-powershell/)) 21 | 22 | 23 | 24 | Get started 25 | ==== 26 | 27 | The easiest way to get started is to fork the repository. 28 | 29 | Contributing 30 | ===== 31 | 32 | If you are interested in collaborating project, feel free to clone or fork the repository. 33 | 34 | ``` 35 | git clone https://github.com/tomaztk/Powershell-scripts-for-PowerBI.git 36 | ``` 37 | 38 | 39 | License 40 | ======= 41 | Scripts are under the MIT license. 42 | 43 | -------------------------------------------------------------------------------- /Scripts/Retrieving-all-users-for-all-workspaces.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Script for retrieving list of all users and their access permissions for all Workspaces in a given organisation / tenant on Power BI Azure. 4 | .DESCRIPTION 5 | Script iterates through all workspaces and return usernames, their access permission for given workspaces in a given Power BI tenant. 6 | 7 | Script must be run as administrator. Modules: MicrosoftPowerBIMgmt.Profile and SqlServer must also be installed under 8 | same user. 9 | Run: Install-Module -Name MicrosoftPowerBIMgmt.Profile -Force 10 | Created by: Tomaz Kastrun, 02 September 2022 11 | .INPUTS 12 | None. Provide only Username and password for Azure subscription 13 | .LINK 14 | Online github repository: https://github.com/tomaztk/Powershell-scripts-for-PowerBI 15 | #> 16 | 17 | 18 | # 1. Login to app.power.bi 19 | $user = "YourAzure.Email@domain.com" 20 | $pass = "YourStrongP422w!!rd" 21 | 22 | 23 | $SecPasswd = ConvertTo-SecureString $pass -AsPlainText -Force 24 | $myCred = New-Object System.Management.Automation.PSCredential($user,$SecPasswd) 25 | 26 | Connect-PowerBIServiceAccount -Credential $myCred 27 | 28 | 29 | 30 | # 2. Get list of users and workspaces 31 | $WorkSpace_Users = Get-PowerBIWorkspace -Scope Organization -Include All -All 32 | 33 | 34 | # 3. Iterate through the users for each workspace (and exclude Personal Workspaces) 35 | $WorkSpace_Users | ForEach-Object { 36 | $Workspace = $_.name 37 | foreach ($User in $_.Users) { 38 | [PSCustomObject]@{ 39 | WorkspaceName = $Workspace 40 | AccessPermission = $User.accessright 41 | UserName =$user.Identifier} 42 | } 43 | } | Select UserName, AccessPermission, WorkspaceName | Where-Object {$Workspace -NotLike "PersonalWorkspace *"} 44 | 45 | -------------------------------------------------------------------------------- /Scripts/Deleting-datasets.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Script for deleting Usage Report Metrics in all workspaces for given organisation / tenant on Azure. 4 | 5 | .DESCRIPTION 6 | Script iterates through all workspaces and looks for GUID of Dataset "Usage Reports Metrics". 7 | If dataset exists, it deletes it. 8 | 9 | Script must be run as administrator. Module: MicrosoftPowerBIMgmt.Profile must also be installed under 10 | same user. 11 | Run: Install-Module -Name MicrosoftPowerBIMgmt.Profile -Force 12 | 13 | Created by: Tomaz Kastrun, 16 August 2022 14 | 15 | 16 | .INPUTS 17 | None. Provide only Username and password for Azure subscription 18 | 19 | .LINK 20 | Online github repository: https://github.com/tomaztk/Powershell-scripts-for-PowerBI 21 | 22 | #> 23 | 24 | 25 | # 1. Login to app.powerbi/azure 26 | $user = "InsertYourAccountEmail" 27 | $pass = "InsertHereYourOwnPassword" 28 | 29 | $SecPasswd = ConvertTo-SecureString $pass -AsPlainText -Force 30 | $myCred = New-Object System.Management.Automation.PSCredential($user,$SecPasswd) 31 | 32 | Connect-PowerBIServiceAccount -Credential $myCred 33 | 34 | 35 | # 2. Get Organization workspaces 36 | $OrWorkSpaces = Get-PowerBIWorkspace -Scope Organization -All | select-object -Property Id, Name, Type | where { $_.Type -eq "Workspace" } 37 | 38 | 39 | # 3. Iterate through list of Work spaces and for each workspace GUID get Usage Metrics GUID 40 | foreach ($WSid in $OrWorkSpaces) 41 | { 42 | $workSpaceID = $WSid.Id 43 | $workSpaceName = $WSid.Name 44 | $DatasetsURL = 'groups/' + $workSpaceID + '/datasets' 45 | 46 | $x = (Invoke-PowerBIRestMethod -Url $DatasetsURL -Method Get) | ConvertFrom-Json 47 | $UsageReportDatasetID = $x.value | where { $_.Name -eq "Usage Metrics Report" } 48 | $UsageReportDatasetID = $UsageReportDatasetID.id 49 | 50 | if ($UsageReportDatasetID -eq $null ) { 51 | Write-Host ("Dataset for Usage Report Statistics in Workspace >"+ $workSpaceName +"< does not exists") 52 | } 53 | else { 54 | 55 | $UsageReportDatasetURL = 'groups/' + $workSpaceID + '/datasets/' + $UsageReportDatasetID 56 | Invoke-PowerBIRestMethod -Url $UsageReportDatasetURL -Method Delete 57 | Write-Host ("Deleting ....") 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /Scripts/Retrieving-all-users-for-all-workspaces-and-reports.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Script for retrieving list of all users and their access permissions on all reports within Workspaces in a given organisation / tenant on Power BI Azure. 4 | .DESCRIPTION 5 | Script iterates through all workspaces and return usernames, iterates through all reports and retrieve access policy with 6 | usernames for given workspaces in a given Power BI tenant. 7 | 8 | Script must be run as administrator. Modules: MicrosoftPowerBIMgmt.Profile and Join-Object must also be installed under 9 | same user. 10 | Run: Install-Module -Name MicrosoftPowerBIMgmt.Profile -Force 11 | Run: Install-Module -Name Join-Object -Force 12 | Created by: Tomaz Kastrun, 13 September 2022 13 | .INPUTS 14 | None. Provide only Username and password for Azure subscription 15 | .LINK 16 | Online github repository: https://github.com/tomaztk/Powershell-scripts-for-PowerBI 17 | #> 18 | 19 | 20 | Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass 21 | Import-Module Join-Object 22 | 23 | 24 | # 1. Login to app.power.bi 25 | $user = "YourAzure.Email@domain.com" 26 | $pass = "YourStrongP422w!!rd" 27 | 28 | 29 | $SecPasswd = ConvertTo-SecureString $pass -AsPlainText -Force 30 | $myCred = New-Object System.Management.Automation.PSCredential($user,$SecPasswd) 31 | 32 | Connect-PowerBIServiceAccount -Credential $myCred 33 | 34 | 35 | # 2. Get list of users and workspaces 36 | $WorkSpace_Users = Get-PowerBIWorkspace -Scope Organization -Include All -All 37 | 38 | 39 | # 3. Iterate through the workspace and get reports in each workspace (exclude Personal Workspaces) 40 | $reposts_WS = $WorkSpace_Users | ForEach-Object { 41 | $Workspace = $_.name 42 | foreach ($Rep in $_.Reports) { 43 | [PSCustomObject]@{ 44 | WorkspaceName = $Workspace 45 | ReportID = $Rep.id 46 | ReportName =$Rep.Name} 47 | } 48 | } | Select ReportID, ReportName, WorkspaceName | Where-Object {$Workspace -NotLike "PersonalWorkspace *"} 49 | 50 | 51 | # 4. Iterate through the workspace and get users with access policy on each workspace (exclude Personal Workspaces) 52 | $users_WS = $WorkSpace_Users | ForEach-Object { 53 | $Workspace = $_.name 54 | foreach ($User in $_.Users) { 55 | [PSCustomObject]@{ 56 | WorkspaceName = $Workspace 57 | AccessPermission = $User.accessright 58 | UserName =$user.Identifier} 59 | } 60 | } | Select UserName, AccessPermission, WorkspaceName | Where-Object {$Workspace -NotLike "PersonalWorkspace *"} 61 | 62 | 63 | # 5. Merge two data 64 | $joinedWS = Join-Object -Left $reposts_WS -Right $users_WS -LeftJoinProperty 'WorkspaceName' -RightJoinProperty 'WorkspaceName' -Type OnlyIfInBoth -LeftProperties ReportName, WorkspaceName -RightProperties UserName, AccessPermission 65 | -------------------------------------------------------------------------------- /Scripts/Storing-activity-logs.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Script for retrieving Activity logs for given organisation / tenant on Power BI Azure. 4 | 5 | .DESCRIPTION 6 | Script iterates through number of days and uses cmdlet Get-PowerBIActivityEvent to retrieve activity log of Power BI tenant. 7 | The logs are stored into SQL Server table with use of cmdlet Write-SqlTableData. 8 | 9 | 10 | Script must be run as administrator. Modules: MicrosoftPowerBIMgmt.Profile and SqlServer must also be installed under 11 | same user. 12 | Run: Install-Module -Name MicrosoftPowerBIMgmt.Profile -Force 13 | Run: Install-Module -Name SqlServer -Force 14 | 15 | Created by: Tomaz Kastrun, 23 August 2022 16 | 17 | 18 | .INPUTS 19 | None. Provide only Username and password for Azure subscription 20 | 21 | .LINK 22 | Online github repository: https://github.com/tomaztk/Powershell-scripts-for-PowerBI 23 | 24 | #> 25 | 26 | 27 | # 1. Login to app.power.bi 28 | $user = "YourAzure.Email@domain.com" 29 | $pass = "YourStrongP422w!!rd" 30 | 31 | $SecPasswd = ConvertTo-SecureString $pass -AsPlainText -Force 32 | $myCred = New-Object System.Management.Automation.PSCredential($user,$SecPasswd) 33 | Connect-PowerBIServiceAccount -Credential $myCred 34 | 35 | # 2. Get Data from app.powerbi/azure for previous day 36 | $Datum = Get-Date((get-date ).AddDays(-1)) -Format "yyyy-MM-dd" 37 | 38 | $StartDate = $Datum + 'T00:00:00' 39 | $EndDate = $Datum + 'T23:59:59' 40 | 41 | 42 | $json = Get-PowerBIActivityEvent -StartDateTime $StartDate -EndDateTime $EndDate | ConvertFrom-Json 43 | $activity = $json | Select Id, CreationTime,Workload, UserId, Activity, ItemName, WorkSpaceName, DatasetName, ReportName, WorkspaceId, ObjectId, DatasetId, ReportId, ReportType ,DistributionMethod, ConsumptionMethod 44 | 45 | # 3. Insert into SQL Server Database 46 | Write-SqlTableData -InputData $activity -ServerInstance "MySQLServer2022" -DatabaseName "MyDatabase" -SchemaName "dbo" -TableName "PowerBIActivityLog" -Force 47 | 48 | 49 | 50 | 51 | ############################################################### 52 | ### If you want to run script for past 10 days, use for loop 53 | ############################################################### 54 | 55 | $user = "YourAzure.Email@domain.com" 56 | $pass = "YourStrongP422w!!rd" 57 | 58 | $SecPasswd = ConvertTo-SecureString $pass -AsPlainText -Force 59 | $myCred = New-Object System.Management.Automation.PSCredential($user,$SecPasswd) 60 | Connect-PowerBIServiceAccount -Credential $myCred 61 | 62 | 63 | $start = 1 64 | $end = 10 # for past 10 days 65 | 66 | for ($i=$start; $i -le $end; $i++) 67 | { 68 | $Datum = Get-Date((get-date ).AddDays(-$i)) -Format "yyyy-MM-dd" 69 | $StartDate = $Datum + 'T00:00:00' 70 | $EndDate = $Datum + 'T23:59:59' 71 | 72 | Write-host("Start with", $StartDate, " and end with ", $EndDate) 73 | 74 | $json = Get-PowerBIActivityEvent -StartDateTime $StartDate -EndDateTime $EndDate | ConvertFrom-Json 75 | $activity = $json | Select Id, CreationTime,Workload, UserId, Activity, ItemName, WorkSpaceName, DatasetName, ReportName, WorkspaceId, ObjectId, DatasetId, ReportId, ReportType ,DistributionMethod, ConsumptionMethod 76 | 77 | # 3. Insert into SQL Server Database 78 | Write-SqlTableData -InputData $activity -ServerInstance "MySQLServer2022" -DatabaseName "MyDatabase" -SchemaName "dbo" -TableName "PowerBIActivityLog" -Force 79 | Write-host("Data for ", $Datum, " inserted into SQL Server Table") 80 | 81 | Start-Sleep -Seconds 120 #wait for 2 minutes 82 | 83 | } -------------------------------------------------------------------------------- /Scripts/Retrieving-all-objects-from-workspaces.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | Make sure to install: 3 | Install-Module MicrosoftPowerBIMgmt -MinimumVersion "1.2.1077" 4 | Install-Module -Name MicrosoftPowerBIMgmt 5 | Install-Module -Name MicrosoftPowerBIMgmt.Admin 6 | Install-Module -Name MicrosoftPowerBIMgmt.Capacities 7 | Install-Module -Name MicrosoftPowerBIMgmt.Workspaces 8 | Install-Module -Name MicrosoftPowerBIMgmt.Data 9 | Install-Module -Name MicrosoftPowerBIMgmt.Reports 10 | Install-Module -Name MicrosoftPowerBIMgmt.Profile 11 | Update-Module -Name MicrosoftPowerBIMgmt 12 | 13 | Get-Module MicrosoftPowerBIMgmt* -ListAvailable | Format-Table 14 | Get-PowerBICapacity 15 | 16 | #> 17 | 18 | ## 19 | ## Storing Power BI data 20 | ## 21 | 22 | 23 | #login! 24 | Login-PowerBI 25 | Connect-PowerBIServiceAccount 26 | 27 | 28 | 29 | 30 | # 31 | # GET PowerBI Workspaces 32 | # 33 | # 34 | $delimiter = ";" 35 | $ALL_workspaces = Get-PowerBIWorkspace | Select-Object Id, Name, Type, CapacityId | ForEach-Object { 36 | $dsID = $_.Id 37 | $dsName = $_.Name 38 | $dsType = $_.Type 39 | $join4 = "$dsID $delimiter $dsName $delimiter $dsType $delimiter $dsID" 40 | $join4 | Format-Table 41 | } 42 | 43 | 44 | # 45 | # GET PowerBI DataFlows 46 | # 47 | # 48 | 49 | #[pscustomobject]@{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"} 50 | 51 | # version 2 52 | $type_df = "DataFlow" 53 | $ALL_Dataflows = Get-PowerBIWorkspace | ForEach-Object { 54 | $wsID = $_.Id 55 | $wsName = $_.Name 56 | Get-PowerBIDataflow -WorkspaceId $wsID | ForEach-Object { 57 | $dsID = $_.Id 58 | $dsName = $_.Name 59 | $join4 = "$dsID $delimiter $dsName $delimiter $type_df $delimiter $wsID" 60 | $join4 | Format-Table 61 | } 62 | } 63 | 64 | 65 | # 66 | # GET PowerBI Datasets 67 | # 68 | 69 | $type_df = "Dataset" 70 | $ALL_Datasets = Get-PowerBIWorkspace | ForEach-Object { 71 | $wsID = $_.Id 72 | $wsName = $_.Name 73 | Get-PowerBIDataset -WorkspaceId $wsID | ForEach-Object { 74 | $dsID = $_.Id 75 | $dsName = $_.Name 76 | $join4 = "$dsID $delimiter $dsName $delimiter $type_df $delimiter $wsID" 77 | $join4 | Format-Table 78 | } 79 | } 80 | 81 | 82 | 83 | # 84 | # GET PowerBI Datasetsource 85 | # 86 | 87 | Get-PowerBIWorkspace | ForEach-Object { 88 | $wsID = $_.Id 89 | $wsName = $_.Name 90 | Write-Host "current workspaceID: " $wsID " and name: " $wsName 91 | Write-Host "Current datasets: " 92 | Get-PowerBIDataset -WorkspaceId $wsID | ForEach-Object { 93 | $dsID = $_.Id 94 | $dsName = $_.Name 95 | #Write-Host "Current dataset: " $dsID " and name: " $dsName 96 | Get-PowerBIDatasource -DatasetId $dsID | Format-Table 97 | } 98 | } 99 | 100 | 101 | 102 | # 103 | # GET PowerBI Reports 104 | # 105 | 106 | $type_ = "Report" 107 | $ALL_Reports = Get-PowerBIWorkspace | ForEach-Object { 108 | $wsID = $_.Id 109 | $wsName = $_.Name 110 | Get-PowerBIReport -WorkspaceId $wsID | ForEach-Object { 111 | $dsID = $_.Id 112 | $dsName = $_.Name 113 | $join4 = "$dsID $delimiter $dsName $delimiter $type_ $delimiter $wsID" 114 | $join4 | Format-Table 115 | } 116 | } 117 | 118 | 119 | 120 | 121 | 122 | 123 | #Export all objects! 124 | #without headers! Headers: ObjectID ObjectName Type WorkspaceID 125 | $initPath = "c:\AllData\" 126 | $filename = "All_objects.csv" 127 | $outPath = $initPath + $filename 128 | $ALL_workspaces | Out-File $outPath -Force 129 | $ALL_Dataflows | Out-File $outPath -Append 130 | $ALL_Datasets | Out-File $outPath -Append 131 | $ALL_Reports | Out-File $outPath -Append 132 | 133 | 134 | #Export only workspaces! 135 | #without headers! Headers: WorkspaceID WorkspaceName Type WorkspaceID 136 | $initPath = "c:\AllData\" 137 | $filename = "All_workspaces.csv" 138 | $outPath = $initPath + $filename 139 | $ALL_workspaces | Out-File $outPath -Force --------------------------------------------------------------------------------