Functions/Export-BsgPbiGatewayDatasource.ps1
<#
.SYNOPSIS Export Power BI gateway datasource to a directory. .DESCRIPTION The metadata of the gateway datasource is saved to a JSON file in a directory. .PARAMETER Source_GatewayId The id of the gateway you would like to export the datasource 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 from. You can find it in the Power BI workspace URL. .PARAMETER Path_Gateway The path to the folder, where the temporary files will be saved. Subfolders "Datasources" and the folder for the datasource will be created automatically. .EXAMPLE # Export gateway datasource Export-BsgPbiGatewayDatasource -Source_GatewayId dc5fb3f4-2ec6-4a1d-b923-edc1a9ce1966 -Source_DatasourceId 9fe443c7-6a2c-4a31-94f2-3e0115974153 -Path_Gateway "C:\temp\BSG PBI Administration\Backup\Gateways\BSG_TEST_MSC" .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-BsgPbiGatewayDatasource{ param ( [Parameter(Mandatory=$true)][guid]$Source_GatewayId, [Parameter(Mandatory=$true)][guid]$Source_DatasourceId, [Parameter(Mandatory=$true)][string]$Path_Gateway ) try{ # Info-Message Write-Host Write-PSFHostColor -Level Host -DefaultColor gray -String " = Exporting <c='white'>datasource</c>..." # Define base URLs and path $BaseUrl = "https://api.powerbi.com/v1.0/myorg" # === # Get datasource (API-Request) # = try{ $RequestUrl = "$BaseUrl/gateways/$Source_GatewayId/datasources/$Source_DatasourceId" $Source_Datasource = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get $Source_Datasource = $Source_Datasource | ConvertFrom-Json } catch{ throw "Error after calling request URL: `"$RequestUrl`"." } # === # Save datasource to JSON file # = if ($Source_Datasource){ # Info-Message $Source_DatasourceName = $Source_Datasource.datasourceName $Source_DatasourceId = $Source_Datasource.id Write-PSFHostColor -Level Host -DefaultColor gray -String " Name: <c='white'>$Source_DatasourceName</c>" # Create temporary directories $Path_Datasources = Join-Path -Path $Path_Gateway -ChildPath "Datasources" $Path_Datasource = Join-Path -Path $Path_Datasources -ChildPath $Source_DatasourceName if((Test-Path $Path_Datasource) -eq $false){ $FileCreatedResponse = New-Item -Path $Path_Datasource -ItemType Directory -ErrorAction SilentlyContinue # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiGateway" -Message "Temp directory created: `"$Path_Datasource`"" } # Define filename $Filename_Metadata = "Datasource.json" $Filename_Mapping = "Mapping_Datasource.json" $Path_DatasourceMetadata = Join-Path -Path $Path_Datasource -ChildPath $Filename_Metadata $Path_DatasourceMapping = Join-Path -Path $Path_Datasource -ChildPath $Filename_Mapping # Save metadata file $Source_Datasource | ConvertTo-Json | Out-File $Path_DatasourceMetadata # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiGateway" -Message " Location: `"$Path_DatasourceMetadata`"" # Save mapping file $Source_Datasource | ConvertTo-Json | Out-File $Path_DatasourceMapping # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiGateway" -Message " Location: `"$Path_DatasourceMapping`"" } else{ Write-Host Write-Warning "No data received." Write-PSFHostColor -Level Host -DefaultColor white -String " Gateway datasource skipped." Write-Host } # === # Export datasource users # = Export-BsgPbiGatewayDatasourceUsers -Source_GatewayId $Source_GatewayId -Source_DatasourceId $Source_DatasourceId -Path_GatewayDatasource $Path_Datasource Write-PSFHostColor -Level Host -DefaultColor green -String ' Gateway datasource exported.' } catch{ Write-Host Stop-PSFFunction -Message "Could not export datasource." -EnableException $true -ErrorRecord $_ } } |