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. It also signs out the sign-in kept for new windows (Connect-VMetric -Persist, Save-VMetricContext): the server revokes it (POST /api/v1/cli/grants/revoke) with every session it started, so the other windows that signed in with it are signed out too, and it is removed from its vault. It does so in a window that never used it as well. A saved API token is removed from the vault and stays valid. .EXAMPLE Disconnect-VMetric .LINK Connect-VMetric .LINK Save-VMetricContext #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] param() $sessions = Get-VMetricCachedSession $savedVault = Read-VMetricSavedContextMarker if ($sessions.Count -eq 0 -and -not $savedVault) { 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 ($savedVault) { $names = @($names) + "the sign-in saved for new windows (vault '$(ConvertTo-VMetricSafeText -Text $savedVault)')" } if (-not $PSCmdlet.ShouldProcess((@($names) -join ', '), 'Sign out')) { return } foreach ($session in $sessions) { Stop-VMetricSessionOnServer -Session $session } Clear-VMetricSessionState # Nothing is loaded in this window after it signed out. $script:VMetricSavedContextState = 'Done' if ($savedVault -and (Remove-VMetricSavedContext)) { Write-Verbose 'The saved sign-in is signed out and removed.' } Write-Verbose 'Signed out.' } |