Functions/Import-BsgPbiGateway.ps1

<#
    .SYNOPSIS
        Import a Power BI gateway to a tenant.
         
    .DESCRIPTION
        The gateway datasources and it's users are imported from a directory with JSON files.
 
    .PARAMETER Path_Gateway
        The path to the folder, where the old gateway metadata is stored.
 
    .EXAMPLE
        # Import gateway
        Import-BsgPbiGateway -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 Import-BsgPbiGateway{

    param
    (
        [Parameter(Mandatory=$true)][string]$Path_Gateway
    )

    try{
        # Info-Message
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String "== Imporing gateway cluster..."

        # Define paths and URLs
        $FileName_GatewayMapping = "Mapping_Gateway.json"
        $Path_Datasources = Join-Path -Path $Path_Gateway -ChildPath "Datasources"
        $Path_GatewayMapping = Join-Path -Path $Path_Gateway -ChildPath $FileName_GatewayMapping
        $BaseUrl = "https://api.powerbi.com/v1.0/myorg"

        # Check path
        if ((Test-Path $Path_Gateway) -eq $false){
            throw "Path `"$Path_Gateway`" does not exist."
        }

        # Get gateway name from path
        $Source_GatewayName = $Path_Gateway | split-path -Leaf

        # Get gateway mapping file
        if ((Test-Path $Path_GatewayMapping) -eq $false){
            throw "File does not exist is folder `"$Path_Gateway`"."
        }
        $GatewayMapping = Get-Content -Path $Path_GatewayMapping | ConvertFrom-Json -ErrorAction Stop


        # Check if gateway cluster exists (new_id is a guid in mapping file)
        $Target_GatewayMapping_Exists = $GatewayMapping.new_id -ne ""
        try{
            $Target_GatewayId = [guid]$GatewayMapping.new_id
        } catch{
            $Target_GatewayMapping_Exists = $false;
        }

        if ($Target_GatewayMapping_Exists){

            # Check if Gatewaycluster exists in PBI Service
            $Source_GatewayName = $GatewayMapping.name
            try{
                $RequestUrl = "$BaseUrl/gateways/$Target_GatewayId"
                $Source_Gateway = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get -ErrorAction Stop
                $Source_Gateway = $Source_Gateway | ConvertFrom-Json
            } catch{
                Write-Host
                Stop-PSFFunction -Message "Gateway cluster with ID `"$Target_GatewayId`" and old name `"$Source_GatewayName`" not found in PBI Service. `nRequest URL: `"$RequestUrl`"."  -EnableException 0 -ErrorRecord $_ -OverrideExceptionMessage
                return
            }

            # Get datasource names from folder
            $Datasource_items = Get-ChildItem -Path $Path_Datasources -Directory -Name

            # Import each datasource
            foreach ($Source_DatasourceName in $Datasource_items) {
                Import-BsgPbiGatewayDatasource -Target_GatewayId $Target_GatewayId -Path_Datasource "$Path_Datasources\$Source_DatasourceName"   
            }

        } else{

            # User need to edit metadata file
            Write-Host
            Stop-PSFFunction -Message "New gateway cluster ID does not exist in mapping file or the ID is not a valid guid." -EnableException 0 -ErrorRecord $_ -OverrideExceptionMessage
            Write-PSFHostColor -Level Host -DefaultColor gray -String "Please install and configure a gateway cluster for the new tenant before restoring (old gateway name: <c='white'>$Source_GatewayName</c>)."
            Write-PSFHostColor -Level Host -DefaultColor gray -String "After the gateway appears in the PBI Service portal, add the new gateway ID (new_id) to the gateway mapping file."
            Write-PSFHostColor -Level Host -DefaultColor gray -String "Mapping file: <c='white'>$Path_GatewayMapping</c>"
            Write-PSFHostColor -Level Host -DefaultColor gray -String "You can find the new gateway ID in the PBI Service URL after navigating to the gateway configuration."
            return

        }

    } catch{
        Write-Host
        Stop-PSFFunction -Message "Could not import gateway." -EnableException $true -ErrorRecord $_
    }
}