Functions/Import-BsgPbiGatewayDatasource.ps1

<#
    .SYNOPSIS
        Import a Power BI datasource to a gateway.
         
    .DESCRIPTION
        The gateway datasource and it's users are imported from a directory with JSON files.
 
    .PARAMETER Target_GatewayId
        The gateway ID of the new gateway cluster.
        You can find the new gateway ID in the Power BI Service URL after you configured the gateway.
 
    .PARAMETER Path_Datasource
        The path to the folder, where the old gateway datasource is stored.
 
    .EXAMPLE
        # Import gateway datasource
        Import-BsgPbiGatewayDatasource -Target_GatewayId "dc5fb3f4-2ec6-4a1d-b923-edc1a9ce1966" -Path_Datasource "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 Import-BsgPbiGatewayDatasource{

    param
    (
        [Parameter(Mandatory=$true)][guid]$Target_GatewayId,
        [Parameter(Mandatory=$true)][string]$Path_Datasource
    )

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

        # Define paths and URLs
        $FileName_DatasourceMapping = "Mapping_Datasource.json"
        $Path_DatasourceMapping = Join-Path -Path $Path_Datasource -ChildPath $FileName_DatasourceMapping
        $BaseUrl = "https://api.powerbi.com/v1.0/myorg"

        # Get datasource name from path
        $Source_DatasourceName = $Path_Datasource | Split-Path -Leaf

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

        # Prepare new request body
        $DatasourceMapping.psobject.properties.remove("id")
        $DatasourceMapping.psobject.properties.remove("@odata.context")
        $DatasourceMapping.psobject.properties.remove("gatewayId")
        $DatasourceMapping
        $Target_DatasourceName = $DatasourceMapping.datasourceName
        $Request_Body = $DatasourceMapping | ConvertTo-Json
        $Request_Body

        # Import datasources
        try{
            $RequestUrl = "$BaseUrl/gateways/$Target_GatewayId/datasources" #?skipTestConnection=true
            $RequestUrl
            $Response = Invoke-PowerBIRestMethod -Url $RequestUrl -Body $Request_Body -Method Post -ErrorAction Stop  -Verbose
            $Response
            Write-Host $Response
            #$Target_DatasourceId = ...
        } catch{
            Write-Host
            Stop-PSFFunction -Message "Error when importing datasource `"$Target_DatasourceName`" and gateway ID `"$Target_GatewayId`"`nRequest URL: `"$RequestUrl`". `n$_"  -EnableException 0 -ErrorRecord $_ -OverrideExceptionMessage
            return
        }
        
        # Import Datasources Users
        #Import-BsgPbiGatewayDatasourceUsers -Target_GatewayId $Target_GatewayId -Target_DatasourceId $Target_DatasourceId -Path_Datasource $Path_Datasource

        Write-PSFHostColor -Level Host -DefaultColor green -String ' Gateway datasource imported.'

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