functions/Get-PBMDatasetRefreshHistory.ps1


<#
.SYNOPSIS
This command returns refresh history information for relevant Power BI Datasets
 
.DESCRIPTION
This can return data for a single dataset, all datasets in a workspace or in all workspaces. Refresh information includes Start & End times,
refresh type (ie. On demand, scheduled) and Status being Completed, failed or in progress.
 
.PARAMETER authToken
This is the required API authentication token (string) generated by the Get-PBIAuthTokenUnattended or Get-PBIAuthTokenPrompt commands.
 
.PARAMETER workspaceID
Optional parameter to restrict data to a specific Workspace ID
 
.PARAMETER workspaceName
Optional parameter to restrict data to a specific Workspace Name. The Workspace ID is retrieved using this name by the function
 
.PARAMETER DatasetID
Optional parameter to only return refresh data for specified dataset ID
 
.PARAMETER TopN
Optional parameter to return the top [Number] of refresh records for the dataset. This is forced to the Top 1 (last refresh) when
no Dataset ID or Workspace is provided
 
.EXAMPLE
Get-PBMDatasetRefreshHistory -authToken $auth -workspaceID 1530055f-XXXX-XXXX-XXXX-ee8c87e4a648
Get-PBMDatasetRefreshHistory -authToken $auth -workspaceName 'Workspace Name'
Get-PBMDatasetRefreshHistory -authToken $auth -workspaceID 1530055f-XXXX-XXXX-XXXX-ee8c87e4a648 -DatasetID ffac73aa-XXXX-XXXX-XXXX-643f36b11a68
Get-PBMDatasetRefreshHistory -authToken $auth -workspaceName 'Workspace Name' -DatasetID ffac73aa-XXXX-XXXX-XXXX-643f36b11a68
 
.NOTES
General notes
#>

function Get-PBMDatasetRefreshHistory {

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $authToken,

        [Alias("id")]
        [string]
        $workspaceID,

        [Alias("name")]
        [string]
        $workspaceName,

        [string]
        $DatasetID,

        [int]
        $TopN = 20
    )

    Begin {

        Write-Verbose 'Building Rest API header with authorization token'
        $authHeader = @{
            'Content-Type'  = 'application/json'
            'Authorization' = 'Bearer ' + $authToken
        }
    }
    Process {

        if (!$workspaceID -and !$workspaceName -and !$DatasetID) {
            Write-Verbose 'Limiting refresh data to top 1 record as no Workspace or Dataset provided. ALL will be returned'
            $TopN = 1
        }

        try {
            if ($DatasetID) {
                Write-Verbose 'Specific Dataset ID Provided'
                if ($workspaceName) {
                    Write-Verbose 'Workspace Name provided. Matching to ID & building API call'
                    $workspace = Get-PBMWorkspace -authToken $authToken -workspaceName $workspaceName
                    $workspaceID = $workspace.id
                    $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspaceID)/datasets/$($datasetID)/refreshes"
                }
                elseif ($workspaceID) {
                    Write-Verbose 'Workspace ID provided. Building API call'
                    $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspaceID)/datasets/$($datasetID)/refreshes"
                }
                else {
                    Write-Warning "Unable to Proceed: No Workspace info provided for Dataset"
                    throw
                }

                Write-Verbose 'Returning refresh history for specified dataset ID'
                $refreshes = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET

                $refreshes.value | Add-Member -NotePropertyName "DatasetID" -NotePropertyValue $datasetID
                $refreshes.value | Add-Member -NotePropertyName "WorkspaceID" -NotePropertyValue $workspaceID
            }
            else {
                Write-Verbose 'No Dataset ID Provided. Returning refresh data for all Datasets'
                if ($workspaceID) {
                    Write-Verbose 'Returning datasets for specified Workspace'
                    $datasets = Get-PBMDataset -authToken $authToken -workspaceID $workspaceID
                }
                elseif ($workspaceName) {
                    Write-Verbose 'Returning datasets for specified Workspace'
                    $datasets = Get-PBMDataset -authToken $authToken -workspaceName $workspaceName
                }
                else {
                    Write-Verbose 'Returning datasets for ALL Workspace'
                    $datasets = Get-PBMDataset -authToken $authToken
                }

                $refreshes = @()

                foreach ($dataset in $datasets) {

                    if ($dataset.isRefreshable -eq $true) { #We can only return refresh info on datasets that can be refreshed
                        Write-Verbose "Returning refresh history for dataset: $($dataset.name)"
                        # Need to escape the $top={N} usage in the API call
                        $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($dataset.WorkspaceID)/datasets/$($dataset.id)/refreshes/?`$top=$($TopN)"

                        $refresh = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET

                        $refresh.value | Add-Member -NotePropertyName "DatasetID" -NotePropertyValue $dataset.id
                        $refresh.value | Add-Member -NotePropertyName "WorkspaceID" -NotePropertyValue $dataset.WorkspaceID
                        $refreshes += $refresh
                    }
                }
            }

        }
        catch {
            Write-Error "Error calling REST API: $($_.Exception.Message)"
        }
    }
    End {

        return $refreshes.Value

    }
}