Functions/Export-BsgPbiWorkspaceMetadata.ps1

<#
    .SYNOPSIS
        Save Power BI workspace metadata to a directory.
         
    .DESCRIPTION
        The workspace metadata is saved in a JSON file to the specified directory.
 
    .PARAMETER Source_WorkspaceId
        The id of the workspace you would like to save.
        You can find it in the Power BI workspace URL.
 
    .PARAMETER Path_Workspace
        The path to the folder, where the workspace metadata should be saved.
 
    .EXAMPLE
        # Export workspace
        Export-BsgPbiWorkspaceMetadata -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -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 Export-BsgPbiWorkspaceMetadata{
    param(
        [Parameter(Mandatory=$true)][string]$Source_WorkspaceId, 
        [Parameter(Mandatory=$true)][string]$Path_Workspace
    )
    
    try{

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

        # Prepare final path
        $Filename_WorkspaceMetadata = "Workspace.json"
        $Filename_WorkspaceMapping = "Mapping_Workspace.json"
        $Path_WorkspaceMetadata = Join-Path -Path $Path_Workspace -ChildPath $Filename_WorkspaceMetadata
        $Path_WorkspaceMapping = Join-Path -Path $Path_Workspace -ChildPath $Filename_WorkspaceMapping


        # ===
        # Get workspace
        # =

        try{

            if ($Source_WorkspaceId -eq "me") { 
                $Source_Workspace = [PSCustomObject]@{
                    "Id" = "me"
                    "Name" = "My Workspace"
                }
            } else { 
                $Source_Workspace = Get-PowerBIWorkspace -Id $Source_WorkspaceId
            }

        } catch{
            throw "Error after calling CMDLET Get-PowerBIWorkspace with -Id `"$Source_WorkspaceId`"."
        }


        # ===
        # Check if workspace exists
        # =

        if (!$Source_Workspace.id){
            throw "No workspace found with workspace id `"$Source_WorkspaceId`"."
        }
        $Source_WorkspaceName = $Source_Workspace.name


        # ===
        # Create temporary directories
        #

        if((Test-Path $Path_Workspace) -eq $false){
            $FileCreatedResponse = New-Item -Path $Path_Workspace -ItemType Directory -ErrorAction SilentlyContinue
            # Write-PSFMessage -Level Verbose -FunctionName "Backup-BsgPbiWorkspace" -Message "Temp directory created: `"$Path_Workspace`""
        }

        # ===
        # Save metadata file
        # =

        $Source_Workspace | ConvertTo-Json | Out-File $Path_WorkspaceMetadata
        # Write-PSFMessage -Level Verbose -FunctionName "Backup-BsgPbiWorkspace" -Message " Location: `"$Path_WorkspaceMetadata`""


        # ===
        # Create mapping file (with new workspace name)
        # =

        try{

            # Add new workspace name to Workspace Metadata
            if ($Source_WorkspaceId -eq 'me'){
                # for My Workspace, new and old name and id are the same
                $Target_Workspace = [PSCustomObject]@{
                    "Id" = $Source_WorkspaceId
                    "Name" = $Source_WorkspaceName
                    "new_name" = $Source_WorkspaceName
                    "new_id" = $Source_WorkspaceId
                    # ? Description is always null - only works with admin API calls?
                    # "description" = $Source_Workspace.description
                }        
            } else {
                $Target_Workspace = [PSCustomObject]@{
                    "Id" = $Source_WorkspaceId
                    "Name" = $Source_WorkspaceName
                    "new_name" = $Source_WorkspaceName
                    # ? Description is always null - only works with admin API calls?
                    # "description" = $Source_Workspace.description
                }
            }
            


            # Save new metadata mapping file to "xyz_Mapping.json"
            $Target_Workspace | ConvertTo-Json | Out-File $Path_WorkspaceMapping

        } catch{
            throw "Error when creating mapping file. $_"
        }

        # Export User-Permissions of workspace
        if ($Source_WorkspaceId -ne 'me'){
            Export-BsgPbiWorkspaceUsers -Source_WorkspaceId $Source_WorkspaceId -Path_Workspace $Path_Workspace
        }

        Write-PSFHostColor -Level Host -DefaultColor green -String ' Workspace metadata exported.'
        
    }catch{
        if ($Source_WorkspaceName){
             -Message "Could not export Workspace metadata `"$Source_WorkspaceName`"." -EnableException $False -Errorrecord $_
            return
        } else{
            Write-Host
            Stop-PSFFunction -Message "Could not export Workspace metadata." -EnableException $False -Errorrecord $_
            return
        }
    }
}