Functions/Export-BsgPbiGateway.ps1
<#
.SYNOPSIS Export Power BI gateway to a directory. .DESCRIPTION The metadata of the gateway is saved to a JSON file in a directory. .PARAMETER Source_GatewayId The id of the gateway you would like to export. You can find it in the Power BI gateway URL. .PARAMETER Path The path to the folder, where the temporary files will be saved. Subfolders "gateways" will be created automatically. .EXAMPLE # Export gateway Export-BsgPbiGateway -Source_GatewayId dc5fb3f4-2ec6-4a1d-b923-edc1a9ce1966 -Path "C:\temp\BSG PBI Administration\Backup" .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-BsgPbiGateway{ param ( [Parameter(Mandatory=$true)][guid]$Source_GatewayId, [Parameter(Mandatory=$true)][string]$Path ) try{ # Info-Message Write-Host Write-PSFHostColor -Level Host -DefaultColor white -String "== Exporting gateway cluster..." # Define base URLs and path $BaseUrl = "https://api.powerbi.com/v1.0/myorg" $Path_Backup = Join-Path -Path $Path -ChildPath "Backup" $Path_Gateways = Join-Path -Path $Path_Backup -ChildPath "Gateways" # === # Get gateway (API-Request) # = try{ $RequestUrl = "$BaseUrl/gateways/$Source_GatewayId" $Source_Gateway = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get $Source_Gateway = $Source_Gateway | ConvertFrom-Json } catch{ throw "Error after calling request URL: `"$RequestUrl`"." } # === # Save gateway to JSON file # = if ($Source_Gateway){ # Info-Message $Source_GatewayName = $Source_Gateway.name $Source_GatewayId = $Source_Gateway.id Write-PSFHostColor -Level Host -DefaultColor gray -String " Name: <c='white'>$Source_GatewayName</c>" # Create temporary directories $Path_Gateway = Join-Path -Path $Path_Gateways -ChildPath $Source_GatewayName if((Test-Path $Path_Gateway) -eq $false){ $FileCreatedResponse = New-Item -Path $Path_Gateway -ItemType Directory -ErrorAction SilentlyContinue # Write-PSFMessage -Level Verbose -Message "Temp directory created: `"$Path_Gateway`"" } # Define filenames $Filename_Metadata = "Gateway.json" $Filename_Mapping = "Mapping_Gateway.json" $Path_GatewayMetadata = Join-Path -Path $Path_Gateway -ChildPath $Filename_Metadata $Path_GatewayMapping = Join-Path -Path $Path_Gateway -ChildPath $Filename_Mapping # Save metadata file $Source_Gateway | ConvertTo-Json | Out-File $Path_GatewayMetadata # Write-PSFMessage -Level Verbose -Message " Location: `"$Path_GatewayMetadata`"" # Edit and save mapping file $Source_Gateway | Add-Member -MemberType NoteProperty -Name "new_id" -value "" $Source_Gateway | ConvertTo-Json | Out-File $Path_GatewayMapping Write-PSFHostColor -Level Host -DefaultColor gray -String " Location: <c='white'>$Path_GatewayMapping</c>" } else{ Write-Host Write-Warning "No data received." Write-PSFHostColor -Level Host -DefaultColor white -String " Gateway skipped." Write-Host } # === # Get gateway datasources (API-Request) # = try{ $RequestUrl = "$BaseUrl/gateways/$Source_GatewayId/datasources" $Source_Datasources = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get $Source_Datasources = $Source_Datasources | ConvertFrom-Json } catch{ throw "Error after calling request URL: `"$RequestUrl`"." } # === # Export each datasource # = foreach ($Datasource in $Source_Datasources.value) { $Source_DatasourceId = $Datasource.id # $Source_DatasourceName = $Datasource.datasourceName Export-BsgPbiGatewayDatasource -Source_GatewayId $Source_GatewayId -Source_DatasourceId $Source_DatasourceId -Path_Gateway $Path_Gateway } } catch{ Write-Host Stop-PSFFunction -Message "Could not export gateway." -EnableException $true -ErrorRecord $_ } } |