Functions/Export-BsgPbiWorkspaceUsers.ps1
<#
.SYNOPSIS Save Power BI workspace users to a directory. .DESCRIPTION The workspace users are saved in a JSON file to the specified directory. .PARAMETER Source_WorkspaceId The id of the workspace you would like to save the users from. You can find it in the Power BI workspace URL. .PARAMETER Path_Workspace The path to the folder, where the workspace users should be saved. .EXAMPLE # Export workspace users Export-BsgPbiWorkspaceUsers -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Path_Workspace "C:\temp\BSG PBI Administration\Backup\Workspaces\BSGroup DA - Test Workspace" .INPUTS .OUTPUTS .NOTES This script uses the Power BI Management module for Windows PowerShell. If this module isn't installed, install it by using the command 'Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser'. #> function Export-BsgPbiWorkspaceUsers{ param( [Parameter(Mandatory=$true)][string]$Source_WorkspaceId, [Parameter(Mandatory=$true)][string]$Path_Workspace ) try{ # Define URLs and path if ($Source_WorkspaceId -eq 'me'){ $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg" } else{ $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg/groups/" + $Source_WorkspaceId } $RequestUrl = $Source_WorkspaceUrl + "/users" $Filename_WorkspaceUsers = "WorkspaceUsers.json" $Path_WorkspaceUsers = Join-Path -Path $Path_Workspace -ChildPath $Filename_WorkspaceUsers # === # Get workspace users (API-call) # try{ # get users $Source_Response = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get -ErrorAction Stop # Log number of users $NumOfUsers = $Source_Response | ConvertFrom-JSON $NumOfUsers = $NumOfUsers.value.count Write-PSFHostColor -Level Host -DefaultColor gray -String " Users: <c='green'>$NumOfUsers</c>" } catch{ throw "Error trying to get Power BI workspace users when calling request URL: `"$RequestUrl`". `nInfo: Check if workspace with ID `"$Source_WorkspaceId`" exists." } # === # Save to JSON file # = if ($Source_Response){ # Create temporary directories if((Test-Path $Path_Workspace) -eq $false){ $FileCreatedResponse = New-Item -Path $Path_Workspace -ItemType Directory -ErrorAction SilentlyContinue # Write-PSFMessage -Level Verbose -FunctionName "Backup-BsgPbiWorkspace" -Message "Temp directory created: `"$Path_Workspace`"" } # Save file $Source_Response | Out-File $Path_WorkspaceUsers # Write-PSFMessage -Level Verbose -FunctionName "Backup-BsgPbiWorkspace" -Message " location: `"$Path_WorkspaceUsers`"" } else{ Write-Host Write-Warning "No data received" Write-PSFHostColor -Level Host -DefaultColor yellow -String " Workspace users skipped." Write-Host } }catch{ Write-Host Stop-PSFFunction -Message "Could not export workspace users." -EnableException $False -Errorrecord $_ return } } |