Public/Get-VMetricTenant.ps1

function Get-VMetricTenant {
    <#
    .SYNOPSIS
    Lists the tenants you can switch to.
 
    .DESCRIPTION
    Get-VMetricTenant lists the tenants your sign-in can use: your organization's main tenant, the
    subtenants you manage as an MSSP, and those you were granted delegated access to. IsCurrent marks the
    one you are in. Switch with Set-VMetricContext.
 
    Signed in with an API token, it answers the token's own tenant, the only one it can use.
 
    .PARAMETER Name
    Only tenants whose name matches (wildcards allowed), or whose id is this.
 
    .EXAMPLE
    Get-VMetricTenant
 
    .EXAMPLE
    Get-VMetricTenant -Name 'Contoso*' | Set-VMetricContext
 
    .OUTPUTS
    VirtualMetric.Tenant
 
    .LINK
    Set-VMetricContext
    #>

    [CmdletBinding()]
    [OutputType('VirtualMetric.Tenant')]
    param(
        [Parameter(Position = 0)]
        [SupportsWildcards()]
        [string] $Name
    )

    try {
        $session = Get-VMetricSession
        if ($session.AuthMethod -eq 'ApiToken') {
            if (-not $session.TenantId) {
                Write-Warning 'The tenant of this API token is not known: reading it needs the SETTINGS_READ permission.'
                return
            }
            $tenants = @([pscustomobject]@{ id = $session.TenantId; name = $session.TenantName })
        }
        else {
            $tenants = Get-VMetricTenantList -Session $session
        }
        foreach ($tenant in $tenants) {
            $object = ConvertTo-VMetricTenantObject -InputObject $tenant -CurrentTenantId $session.TenantId
            if (-not $Name -or $object.Name -like $Name -or $object.Id -eq $Name) {
                $object
            }
        }
    }
    catch {
        if (Test-VMetricFlowControl -ErrorRecord $_) {
            throw
        }
        Write-VMetricCmdletError -Cmdlet $PSCmdlet -ErrorRecord $_
    }
}