Public/Get-DuneTenant.ps1

<#
.SYNOPSIS
Retrieve Dune tenants.

.DESCRIPTION
Gets tenant objects from Dune. Optionally filter by tenant id and request raw API output. Returns `DuneTenant` objects by default.

.PARAMETER TenantId
The GUID of a tenant. Use the `Tenant` parameter set (ValueFromPipelineByPropertyName) to filter results.

.PARAMETER Raw
If set, returns raw API objects instead of `DuneTenant` objects.

.EXAMPLE
PS> Get-DuneTenant
Returns all tenants.

.EXAMPLE
PS> Get-DuneTenant -TenantId 3d8f6b5a-...
Returns the tenant with the specified `TenantId`.
#>

function Get-DuneTenant {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(ParameterSetName = "Tenant", ValueFromPipelineByPropertyName)]
        [guid]$TenantId,

        [Parameter()]
        [switch]$Raw
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $Uri = "tenants"
        $Method = "GET"
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        try {
            $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method
            $Results = if ($Response.Content) { $Response.Content | ConvertFrom-Json }
            $ReturnObjects += $Results | ForEach-Object {
                if ($Raw) {
                    $_
                }
                else {
                    ConvertTo-DuneClassObject -Class DuneTenant -InputObject $_
                }
            }
        }
        catch {
            throw $_
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        # Filters
        switch ($PSCmdlet.ParameterSetName) {
            'Tenant' { $ReturnObjects = $ReturnObjects | Where-Object id -EQ $TenantId }
            Default {}
        }
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object Name
    }
}