Functions/Export-BsgPbiGatewayDatasourceUsers.ps1
<#
.SYNOPSIS Export Power BI gateway datasource users to a directory. .DESCRIPTION The gateway datasource users are saved to a JSON file in a directory. .PARAMETER Source_GatewayId The id of the gateway you would like to export the datasource users from. You can find it in the Power BI workspace URL. .PARAMETER Source_DatasourceId The id of the gateway you would like to export the datasource users from. You can find it in the Power BI workspace URL. .PARAMETER Path_GatewayDatasource The path to the folder, where the temporary files will be saved. .EXAMPLE # Export gateway datasource users Export-BsgPbiGatewayDatasourceUsers -Source_GatewayId dc5fb3f4-2ec6-4a1d-b923-edc1a9ce1966 -Source_DatasourceId 9fe443c7-6a2c-4a31-94f2-3e0115974153 -Path_GatewayDatasource "C:\temp\BSG PBI Administration\Backup\Gateways\BSG_TEST_MSC\Datasources\SQLDB_localhost_AdventureWorksDW2017" .INPUTS .OUTPUTS .NOTES This script uses the Power BI Management module for Windows PowerShell. If this module is not installed, install it by using the command 'Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser'. #> function Export-BsgPbiGatewayDatasourceUsers{ param ( [Parameter(Mandatory=$true)][guid]$Source_GatewayId, [Parameter(Mandatory=$true)][guid]$Source_DatasourceId, [Parameter(Mandatory=$true)][string]$Path_GatewayDatasource ) try{ # Define base URLs and path $BaseUrl = "https://api.powerbi.com/v1.0/myorg" # === # Get datasource users (API-Request) # = try{ # Get users $RequestUrl = "$BaseUrl/gateways/$Source_GatewayId/datasources/$Source_DatasourceId/users" $Source_DsUsers = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get $Source_DsUsers = $Source_DsUsers | ConvertFrom-Json $Source_DsUsers = $Source_DsUsers.value # Log number of users $NumOfUsers = $Source_DsUsers.count Write-PSFHostColor -Level Host -DefaultColor gray -String " Users: <c='green'>$NumOfUsers</c>" } catch{ throw "Error after calling request URL: `"$RequestUrl`"." } # === # Save datasource users to JSON file # = if ($Source_DsUsers){ # Create temporary directories if((Test-Path $Path_GatewayDatasource) -eq $false){ $FileCreatedResponse = New-Item -Path $Path_GatewayDatasource -ItemType Directory -ErrorAction SilentlyContinue # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiGateway" -Message "Temp directory created: `"$Path_GatewayDatasource`"" } # Define filename $Filename = "DatasourceUsers.json" $Path_UsersMetadata = Join-Path -Path $Path_GatewayDatasource -ChildPath $Filename # Save metadata file $Source_DsUsers | ConvertTo-Json | Out-File $Path_UsersMetadata # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiGateway" -Message " Location: `"$Path_UsersMetadata`"" } else{ Write-Host Write-Warning "No data received." Write-PSFHostColor -Level Host -DefaultColor white -String " Gateway datasource users skipped." Write-Host } } catch{ Write-Host Stop-PSFFunction -Message "Could not export datasource users." -EnableException $true -ErrorRecord $_ } } |