Public/Disconnect-VMetric.ps1

function Disconnect-VMetric {
    <#
    .SYNOPSIS
    Signs out of VirtualMetric.
 
    .DESCRIPTION
    Disconnect-VMetric ends every session this PowerShell session holds: each signed-in session is revoked on
    the server (POST /api/v1/cli/logout), then all of them are forgotten and their tokens zeroed. An API
    token is only forgotten; revoke the token itself in the console.
 
    .EXAMPLE
    Disconnect-VMetric
 
    .LINK
    Connect-VMetric
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
    param()

    $sessions = Get-VMetricCachedSession
    if ($sessions.Count -eq 0) {
        Write-Verbose 'Not connected.'
        return
    }
    $names = foreach ($session in $sessions) {
        if ($session.TenantName) {
            "$($session.ConsoleUrl) ($(ConvertTo-VMetricSafeText -Text ([string]$session.TenantName)))"
        }
        else {
            [string]$session.ConsoleUrl
        }
    }
    if (-not $PSCmdlet.ShouldProcess((@($names) -join ', '), 'Sign out')) {
        return
    }
    foreach ($session in $sessions) {
        Stop-VMetricSessionOnServer -Session $session
    }
    Clear-VMetricSessionState
    Write-Verbose 'Signed out.'
}