Functions/Restore-BsgPbiTenant.ps1

<#
    .SYNOPSIS
        Restore a Power BI tenant from a local directory.
         
    .DESCRIPTION
        The objects and data of a Power BI tenant will be restored from a directory.
        Before you restore the tenant, please use our backup functions to make a backup of the old tenant.
        After calling the function you will be able to login to the tenant where the objects should be restored.
 
 
    .PARAMETER Path
        The path to the root folder, where the temporary files are stored.
 
    .EXAMPLE
        # Restore Tenant
        Restore-BsgPbiTenant -Path "C:\temp\BSG PBI Administration"
         
    .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 Restore-BsgPbiTenant{

    param
    (
        [Parameter(Mandatory=$true)][string]$Path
    )

    try {

        Write-Host
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String "---------------------------------------------------------------------------------------------"
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor green -String " Restoring tenant... "
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String "---------------------------------------------------------------------------------------------"
        Write-Host
        Write-Host


        # Login and confirm tenant
        Write-PSFHostColor -Level Host -DefaultColor gray -String "Choose the tenant and user to start the restoration..."
        Write-PSFHostColor -Level Host -DefaultColor gray -String "You need to be a <c='white'>Power BI Administrator</c> to restore the tenant."
        try{
            Logout-PowerBI
            $PbiLogin = Login-PowerBI -ErrorAction Stop
        } catch{
            throw "Error trying to connect to Power BI tenant."
        }
        $TenantId = $PbiLogin.TenantId
        $UserName = $PbiLogin.UserName
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor gray -String "You are logged in with user <c='white'>$userName</c> in tenant <c='white'>$TenantId</c>."
        Write-PSFHostColor -Level Host -DefaultColor white -String "Would you like to start the restoration? [y/n]: "
        $confirmTenant = Read-Host
        Write-Host
        if ($confirmTenant -eq 'y'){
            # continue...
        } else{
            Write-PSFHostColor -Level Host -DefaultColor white -String "Restoration stopped."
            Logout-PowerBI
            return
        }

        # Check if there are datasets without owner (cannot be restored)
        $DatasetsWithoutOwner = Get-BsgPbiDatasetsWithoutOwner -Path $Path -SkipConfirmationMessage $true
        if ($DatasetsWithoutOwner.count -ge 1){
            Write-Host
            Write-Warning "You have $($DatasetsWithoutOwner.count) datasets in your backup which have no report owner."
            Write-Host
            Write-PSFHostColor -Level Host -DefaultColor gray -String "Each dataset needs to have a report bound to the dataset, otherwise the data cannot be exported and restored."
            Write-PSFHostColor -Level Host -DefaultColor gray -String "Please create a report for each dataset which has no owner."
            Write-Host
            Write-PSFHostColor -Level Host -DefaultColor red -String "Datasets without owner:"
            foreach ($dataset in $DatasetsWithoutOwner){
                if ($dataset.workspaceName -ne $LastWorkspace){
                    Write-Host
                    Write-PSFHostColor -Level Host -DefaultColor white -String $($dataset.workspaceName)
                }
                Write-PSFHostColor -Level Host -DefaultColor gray -String "- $($dataset.name)"
                $LastWorkspace = $dataset.workspaceName
            }
            Write-Host
            $confirmDatasetsWithoutOwner = Read-Host "Would you like to continue the restoration anyways? [y/n]"
            Write-Host
            if ($confirmDatasetsWithoutOwner -eq 'y'){
                # continue
            } else {
                Write-PSFHostColor -Level Host -DefaultColor white -String "Restoration stopped."
                Write-PSFHostColor -Level Host -DefaultColor gray -String "Please create a report for each dataset or delete it, then make a backup and start the restoration process again."
                Logout-PowerBI
                return
            }

        }
        
        # Define path to workspaces
        $Path_Backup = Join-Path -Path $Path -ChildPath "Backup"
        $Path_Workspaces = Join-Path -Path $Path_Backup -ChildPath "Workspaces"

        # Get workspace folder names
        $Source_Workspaces = Get-ChildItem -Path $Path_Workspaces -Name

        # ===
        # Loop through workspace folders
        # =
        foreach ($Source_WorkspaceName in $Source_Workspaces) {

            # ===
            # Restore workspace
            # =

            Restore-BsgPbiWorkspace -Source_WorkspaceName $Source_WorkspaceName -Path $Path -PbiConnection $PbiLogin
            
        }


        # ===
        # Import tenant level reports (reports which are live connected to other workspaces datasets)
        # =

        Import-BsgPbiReportsTenantLevel -Path $Path -PbiConnection $PbiLogin


        Write-Host
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor white -String "---------------------------------------------------------------------------------------------"
        Write-Host
        Write-PSFHostColor -Level Host -DefaultColor green -String " Tenant restoration completed. "
        Write-PSFHostColor -Level Host -DefaultColor gray -String " Developed by <c='white'>BSGroup Data Analytics AG</c>"
        Write-PSFHostColor -Level Host -DefaultColor white -String "---------------------------------------------------------------------------------------------"
        Write-Host
        Write-Host
        
    } catch{
        Write-Host
        Stop-PSFFunction -Message "Failed to restore tenant." -EnableException $False -Errorrecord $_
    }
}