Functions/Export-BsgPbiDataset.ps1

<#
    .SYNOPSIS
        Export Power BI datasets to a directory.
         
    .DESCRIPTION
        The metadata of the datasets are saved to a JSON file in a directory.
 
    .PARAMETER Source_WorkspaceId
        The id of the workspace you would like to export the datasets from.
        You can find it in the Power BI workspace URL.
 
    .PARAMETER Source_DatasetId
        The id of the dataset you would like to save the metadata from.
        You can find it in the Power BI dataset URL.
     
    .PARAMETER Path_Workspace
        The path to the folder, where the temporary files will be saved.
        Subfolder "datasets" will be created automatically.
 
    .EXAMPLE
        # Export datasets
        Export-BsgPbiDataset -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Source_DatasetId 1790b587-2ded-486b-9a00-d019ac495a23 -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 is not installed, install it by using the command 'Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser'.
#>


function Export-BsgPbiDataset{

    param
    (
        [Parameter(Mandatory=$true)][string]$Source_WorkspaceId, 
        [Parameter(Mandatory=$true)][guid]$Source_DatasetId, 
        [Parameter(Mandatory=$true)][string]$Path_Workspace
    )

    try{
        # Info-Message
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String  "== Exporting dataset metadata..."

        # Define base URLs and path
        if ($Source_WorkspaceId -eq 'me'){
            $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg"
        } else{
            $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg/groups/" + $Source_WorkspaceId
        }
        $Source_DatasetUrl = $Source_WorkspaceUrl + "/datasets/" +  $Source_DatasetId
        $RequestUrl = $Source_DatasetUrl
        $Path_Datasets = Join-Path -Path $Path_Workspace -ChildPath "Datasets"


        # ===
        # Get dataset (API-Request)
        # =

        try{
            $Source_Dataset = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get
            $Source_Dataset = $Source_Dataset | ConvertFrom-Json
            $Source_DatasetName = $Source_Dataset.name
        } catch{
            throw "Error after calling request URL: `"$RequestUrl`"."
        }

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


        # ===
        # Save to JSON file
        # =

        if ($Source_Dataset){
            
            # Create temporary directories
            if((Test-Path $Path_Datasets) -eq $false){
                $FileCreatedResponse = New-Item -Path $Path_Datasets -ItemType Directory -ErrorAction SilentlyContinue
                # Write-PSFMessage -Level Verbose -Message "Temp directory created: `"$Path_Datasets`""
            }
            
            # Create filename
            $Filename_DatasetMetadata = "Datasets.json"
            $Path_DatasetMetadata = Join-Path -Path $Path_Datasets -ChildPath $Filename_DatasetMetadata

            # Create object array
            if ((Test-Path $Path_DatasetMetadata) -eq $true){
                # existing file
                $DatasetMetadata = @()
                $DatasetMetadata += Get-Content -Path $Path_DatasetMetadata | ConvertFrom-Json
                if (($DatasetMetadata.id -contains $Source_DatasetId) -eq $false){
                    $DatasetMetadata += $Source_Dataset
                } else{
                    # should not happen (if old backup was deleted)
                }
            } else{
                # new file
                $DatasetMetadata = @()
                $DatasetMetadata += $Source_Dataset
            }

            # Save file
            $DatasetMetadata | ConvertTo-Json | Out-File $Path_DatasetMetadata
            # Write-PSFMessage -Level Verbose -Message " Location: `"$Path_DatasetMetadata`""

        } else{
            Write-Host
            Write-Warning "No data received."
            Write-PSFHostColor -Level Host -DefaultColor gray -String "<c='white'>Dataset skipped</c>."
            Write-Host
            return
        }

        # ===
        # Export Schedule
        # =

        Export-BsgPbiDatasetSchedule -Source_WorkspaceId $Source_WorkspaceId -Source_DatasetId $Source_DatasetId -Path_Datasets $Path_Datasets

        Write-PSFHostColor -Level Host -DefaultColor green -String ' Dataset metadata exported.'
        
    } catch{
        Write-Host
        Stop-PSFFunction -Message "Could not export datasets." -EnableException $true -ErrorRecord $_
    }
}