functions/Get-PBMDataset.ps1


<#
.SYNOPSIS
Returns Power BI dataset information
 
.DESCRIPTION
This will return information on a single dataset if a dataset name is provided or onall datasets in a
 specified Workspace or in all workspaces if no Workspace parameter is provided.
 
.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 datasetName
Optional parameter to search for a specific dataset by Name. The Dataset ID is retrieved using
 this name by the function
 
.EXAMPLE
Get-PBMDataset -authToken $auth
 
Get-PBMDataset -authToken $auth -workspaceID 1530055f-XXXX-XXXX-XXXX-ee8c87e4a648
 
Get-PBMDataset -authToken $auth -workspaceName 'Workspace Name'
 
Get-PBMDataset -authToken $auth -datasetName 'Dataset Name'
.NOTES
General notes
#>

function Get-PBMDataset {

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, HelpMessage = "Auth token provided by Get-PBIAuthTokenPrompt")]
        [string]
        $authToken,

        [string]
        $workspaceID,

        [string]
        $workspaceName,

        [string]
        $datasetName
    )

    Begin {

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

        try {
            if ($workspaceID) {
                Write-Verbose 'Returning datasets for specified Workspace'
                $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspaceID)/datasets"
                $datasets = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET

                $datasets.value | Add-Member -NotePropertyName "workspaceID" -NotePropertyValue $workspaceID
            }
            elseif ($workspaceName) {
                Write-Verbose 'Workspace Name provided. Matching to ID & building API call'
                $workspace = Get-PBMWorkspace -authToken $authToken -workspaceName $workspaceName

                Write-Verbose 'Returning datasets for specified Workspace'
                $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspace.id)/datasets"
                $datasets = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET

                $datasets.value | Add-Member -NotePropertyName "workspaceID" -NotePropertyValue $workspace.id
            }
            else {
                Write-Verbose 'No Workspace info provided. Fetching all Workspaces'
                $workspaces = Get-PBMWorkspace -authToken $authToken

                $datasets = @()

                Write-Verbose 'Returning datasets for all Workspaces'
                foreach ($workspace in $workspaces) {
                    $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspace.id)/datasets"
                    $workspaceDatasets = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET

                    $workspaceDatasets.value | Add-Member -NotePropertyName "workspaceID" -NotePropertyValue $workspace.id

                    $datasets += $workspaceDatasets
                }
            }

            if ($datasetName) {
                Write-Verbose "Searching for dataset: '$($datasetName)'"
                $datasets = $datasets.Value | Where-Object {$_.name -eq $datasetName}
                Write-Verbose "Dataset found with ID: '$($datasets.id)'"
            }
            else {
                $datasets = $datasets.Value
            }

        }
        catch [System.Net.WebException] {
            Write-Warning "Permissions not valid to get dataset info on $($uri). Continuing."
        }
        catch {
            Write-Error "Error calling REST API on $($uri): $($_.Exception)"
        }
    }
    End {

        return $datasets

    }
}