functions/Get-Dataset.ps1


<#
.SYNOPSIS
Returns Power BI dataset information
 
.DESCRIPTION
This will return information on all 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
 
.EXAMPLE
Get-Dataset -authToken $auth
Get-Dataset -authToken $auth -workspaceID 1530055f-XXXX-XXXX-XXXX-ee8c87e4a648
Get-Dataset -authToken $auth -workspaceName 'Workspace Name'
 
.NOTES
General notes
#>

function Get-Dataset{
    
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]
        $authToken,
    
        [string]
        $workspaceID,

        [string]
        $workspaceName
    )

    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-Workspace -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 'Fetching all Workspaces'
                $workspaces = Get-Workspace -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
                    
                }
            }               
            
        }
        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.Value

    }
}