diff --git a/Add-User-All-Workspace.ps1 b/Add-User-All-Workspace.ps1 new file mode 100644 index 0000000..ae95ac6 --- /dev/null +++ b/Add-User-All-Workspace.ps1 @@ -0,0 +1,48 @@ +# This script adds the specified user to +# the Workspace mentioned. If no Workspace +# is mentioned, it would add to all the Workspaces + +[cmdletbinding()] +param ( + [Parameter(Mandatory=$true)][string]$UserEmail, + [Parameter][string]$Workspace, + [Parameter][string]$AccessRight +) + +# Instructions: +# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell) +# and the PowerShell cmdlets (Install-Module -Name MicrosoftPowerBIMgmt) +# 2. Run PowerShell as an administrator +# 3. Change PowerShell directory to where this script is saved +# 4. > ./Add-User-All-Workspace.ps1 + +try { + + # Authentication and Header + Connect-PowerBIServiceAccount + $headers = Get-PowerBIAccessToken + + # Defaults to Admin is access right is not specified + if(!$AccessRight) { + $AccessRight = 'Admin' + } + + # If a Workspace is specified, if not all the available Workspace is taken + if($Workspace) { + $Workspace = Get-PowerBIWorkspace -Scope Organization -Name $Workspace + $target_group_id = $Workspace.Id + Add-PowerBIWorkspaceUser -Scope Organization -Id $target_group_id -UserEmailAddress $UserEmail -AccessRight $AccessRight + } + else { + $Workspaces = Get-PowerBIWorkspace -Scope Organization -Filter "Type eq 'Workspace'" + Foreach($Workspace in $Workspaces) { + Write-Host $Workspace.Name + $target_group_id = $Workspace.Id + Add-PowerBIWorkspaceUser -Scope Organization -Id $target_group_id -UserEmailAddress $UserEmail -AccessRight $AccessRight + } + } +} catch { + Write-Host $_.Exception + Write-Host Resolve-PowerBIError -Last + Break +} \ No newline at end of file diff --git a/README.md b/README.md index 08ee557..935ab6a 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,13 @@ This repo contains samples for calling the Power BI REST APIs using PowerShell. * [Zero-Downtime-Capacity-Scale.ps1](https://github.com/Azure-Samples/powerbi-powershell/blob/master/Zero-Downtime-Capacity-Scale.ps1) - scale Power BI Embedded capacity resource, up or down, with no downtime (i.e. embedded content is available during the scaling process). +* [uploadapplication.ps1](https://github.com/Azure-Samples/powerbi-powershell/blob/master/uploadapplication.ps1) - Upload local Power BI Application to the specified Workspace + +* [Add-User-All-Workspace.ps1](https://github.com/Azure-Samples/powerbi-powershell/blob/master/Add-User-All-Workspace.ps1) - Add specified user to mentioned or all the Workspace + ## Prerequisites -To run the scripts in this repo, please install [PowerShell](https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell) and the [Azure Resource Manager PowerShell cmdlets](https://www.powershellgallery.com/packages/AzureRM/). +To run the scripts in this repo, please install [PowerShell](https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell), [Azure Resource Manager PowerShell cmdlets](https://www.powershellgallery.com/packages/AzureRM/) and the [Microsoft Power BI Cmdlets](https://docs.microsoft.com/en-us/powershell/power-bi/overview) . ## Contributing diff --git a/uploadapplication.ps1 b/uploadapplication.ps1 new file mode 100644 index 0000000..93dfef3 --- /dev/null +++ b/uploadapplication.ps1 @@ -0,0 +1,87 @@ +# This script calls the Power BI API to programmatically upload the local PBIX files +# into a Workspace + +[cmdletbinding()] +param ( + [Parameter(Mandatory=$true)][string]$Workspace +) + +# Instructions: +# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell) +# and Azure PowerShell cmdlets (Install-Module AzureRM) and the Microsoft Power BI Cmdlets (https://docs.microsoft.com/en-us/powershell/power-bi/overview) +# 2. Run PowerShell as an administrator +# 3. Change PowerShell directory to where this script is saved +# 4. > ./uploadApplication.ps1 - Workspace + +# Report Folder +# Local Folder which has the PBIX files. The folder has to be created and PBIX files has to be placed in that +# Default Path is Reports + +$report_path_root = "$PSScriptRoot\Reports" + +# End Parameters ======================================= + +# PART 1: Authentication +# ================================================================== +Connect-PowerBIServiceAccount + +# Getting Header with Token +$auth_header = Get-PowerBIAccessToken + + +# PART 2: Checking the Workspace if it exists, if not creating it +# ================================================================== + +try +{ + $target_group_name = $Workspace + # Checking if the Workspace already exist that the user can access + $reponse = Get-PowerBIWorkspace -Name "$target_group_name" -Scope Individual + $target_group_id = $response.id + + if(!$target_group_id) { + # Checking if the Workspace exist in the Organization + $reponse = Get-PowerBIWorkspace -Name "$target_group_name" -Scope Organization + $target_group_id = $response.id + if (!$target_group_id) { + # Creating the Workspace + $body = "{`"name`":`"$target_group_name`"}" + $response = Invoke-PowerBIRestMethod -Url 'groups' –Headers $auth_header –Method POST -Body $body + $target_group_id = $response.id + } + else { + # TODO: add logic to add the user to the Workspace + "Please add the Service user to the Workspace or try again with the user who has access to the Workspace" + Break + } + } +} catch { + "Could not find or create a group with that name. Please try again" + "More details: " + Write-Host $_.Exception + Write-Host Resolve-PowerBIError -Last + Break +} + + +# PART 3: Copying reports and datasets using Export/Import PBIX APIs +# ================================================================== + +$reports = Get-ChildItem $report_path_root + +# import the reports that are built on PBIXes +Foreach($report in $reports) { + + $report_name = $report.Name + $temp_path = "$report_path_root\$report_name" + + try { + New-PowerBIReport -Path '$temp_path' -Name '$report_name' -ConflictAction CreateOrOverwrite -WorkspaceId '$target_group_id' + + } catch [Exception] { + Write-Host $_.Exception + Write-Host "== Error: failed to import PBIX" + Write-Host Resolve-PowerBIError -Last + continue + } +} \ No newline at end of file