Functions/Import-BsgPbiDataset.ps1

<#
    .SYNOPSIS
        Import Power BI dataset metadata from a json file.
         
    .DESCRIPTION
        The dataset metadata is imported from a JSON file into a workspace.
 
    .PARAMETER Target_WorkspaceId
        The id of the workspace you would like to import the dataset metadata.
        You can find it in the Power BI workspace URL.
 
    .PARAMETER Target_DatasetId
        The id of the target dataset, where you would like to import the metadata.
        You can find it in the Power BI dataset URL.
 
    .PARAMETER Source_DatasetId
        The id of the source dataset of which the metadata was exported.
        You can find it in the Power BI dataset URL or in the exported file.
 
    .PARAMETER Path_Workspace
        The path to the workspace folder, where the metadata information is stored.
        Subfolders will be created automatically.
 
    .EXAMPLE
        # Import dataset metadata
        Import-BsgPbiDataset -Target_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Target_DatasetId 4837389a-a104-4701-8039-1bb4645644d7 -Source_DatasetId 827f8cd5-808d-4ef2-883e-e8730a39cf06 -Path_Workspace "C:\temp\BSG PBI Administration\Backup\Workspaces\BSGroup DA - Test Workspace"
 
    .INPUTS
 
    .OUTPUTS
 
    .NOTES
        This script uses the Power BI Management module for Windows PowerShell. If this module isn't installed, install it by using the command 'Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser'.
#>


function Import-BsgPbiDataset{
    param(
        [Parameter(Mandatory=$true)][string]$Target_WorkspaceId, 
        [Parameter(Mandatory=$true)][guid]$Target_DatasetId,
        [Parameter(Mandatory=$true)][guid]$Source_DatasetId,
        [Parameter(Mandatory=$true)][string]$Path_Workspace
    )
    
    try{

        # Check workspace id
        if ($Target_WorkspaceId -ne 'me'){
            $Target_WorkspaceId = [guid]$Target_WorkspaceId
        }

        # Define paths, filenames and URLs
        $Path_Datasets = Join-Path -Path $Path_Workspace -ChildPath 'Datasets'

        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String "== Importing dataset metadata..."

        # Define dataset metadata file
        $FileName_DatasetMetadata = "Datasets.json"
        $Path_DatasetMetadata = Join-Path -Path $Path_Datasets -ChildPath $FileName_DatasetMetadata


        # ===
        # Get dataset metadata file
        # =

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

            # Check mapping file
            if ((Test-Path $Path_DatasetMetadata) -eq $false){
                throw "File does not exist is folder `"$Path_Datasets`"."
            }

            # Get dataset metadata from file
            $DatasetObject = Get-Content -Path $Path_DatasetMetadata | ConvertFrom-Json -ErrorAction Stop
            $DatasetObject = $DatasetObject | Where-Object {$_.id -eq $Source_DatasetId}
            $Source_DatasetName = $DatasetObject.name
            $Is_Refreshable = $DatasetObject.isRefreshable

            # Info-Message
            Write-PSFHostColor -Level Host -DefaultColor gray -String " Name: <c='white'>$Source_DatasetName</c>"

        } catch{
            throw "Error while getting dataset metadata file `"$FileName_DatasetMetadata`". `n$_"   
        }



        # ===
        # Update schedule depending on type of schedule
        # =
        
        if ($Is_Refreshable){

            # ===
            # Import schedule
            # =

            Import-BsgPbiDatasetSchedule -Target_WorkspaceId $Target_WorkspaceId -Target_DatasetId $Target_DatasetId -Source_DatasetId $Source_DatasetId -Path_Workspace $Path_Workspace -Is_Refreshable $true -ErrorAction Stop

        } else{

            # ===
            # DirectQuery schedule
            # =

            Import-BsgPbiDatasetSchedule -Target_WorkspaceId $Target_WorkspaceId -Target_DatasetId $Target_DatasetId -Source_DatasetId $Source_DatasetId -Path_Workspace $Path_Workspace -Is_Refreshable $false -ErrorAction Stop

        }

        Write-PSFHostColor -Level Host -DefaultColor green -String ' Dataset metadata updated.'

    }catch{
        if ($Source_DatasetName){
            Write-Host
            Stop-PSFFunction -Message "Could not import dataset metadata `"$Source_DatasetName`"." -EnableException $False -Errorrecord $_
            return
        } else{
            Write-Host
            Stop-PSFFunction -Message "Could not import dataset metadata." -EnableException $False -Errorrecord $_
            return
        }
    }
}