Functions/Import-BsgPbiGatewayDatasourceUsers.ps1

<#
    .SYNOPSIS
        Import a Power BI users to a gateway datasource.
         
    .DESCRIPTION
        The users are imported from a directory with JSON files into a PBI datasource of a gateway cluster.
 
    .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 Target_DatasourceId
        The datasource ID of the new datasource in a gateway cluster.
        You can find the new datasource ID in the Power BI Service URL after you configured the gateway and added a datasource.
 
    .PARAMETER Path_Datasource
        The path to the folder, where the old gateway datasource is stored.
 
    .EXAMPLE
        # Import datasource users
        Import-BsgPbiGatewayDatasourceUsers -Target_GatewayId "dc5fb3f4-2ec6-4a1d-b923-edc1a9ce1966" -Target_DatasourceId "9fe443c7-6a2c-4a31-94f2-3e0115974153" -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-BsgPbiGatewayDatasourceUsers{

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

    try{
        # Info-Message
        Write-Host "Import-BsgPbiGatewayDatasourceUsers"

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

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

        # TODO: Add Datasource User (API-Request)
        try{
            $RequestUrl = "$BaseUrl/gateways/$Target_GatewayId/datasources/$Target_DatasourceId/users"
            $RequestUrl
            $Response = Invoke-PowerBIRestMethod -Url $RequestUrl -Body $Request_Body -Method Post -ErrorAction Stop
            $Response
            Write-Host $Response
        } 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
        }

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