Public/Save-VMetricContext.ps1

function Save-VMetricContext {
    <#
    .SYNOPSIS
    Keeps the current sign-in for new PowerShell windows.
 
    .DESCRIPTION
    Save-VMetricContext keeps the current context's sign-in in your default Microsoft.PowerShell.SecretManagement
    vault, so that the first VirtualMetric cmdlet a new PowerShell window runs signs in with it, without a
    browser. Connect-VMetric -Persist does the same as it signs in.
 
    What is kept is not this session's tokens, which a new window could not share (a refresh token is used
    once), but a saved sign-in the server issues for this sign-in (POST /api/v1/cli/grants). Each window
    exchanges it for a session and a refresh chain of its own, so none signs another out. It acts as you in this
    context's tenant, ends with this sign-in (30 days after it at most), and is listed with your PowerShell
    sessions in the console, where revoking it signs out every window that used it. A context connected with
    an API token keeps the token itself.
 
    It replaces a sign-in kept before: the server retires that one, and the windows already signed in with it
    stay signed in. Disconnect-VMetric signs the saved sign-in out and removes it.
 
    It needs Microsoft.PowerShell.SecretManagement and a registered vault (Register-SecretVault); the error says
    how to set one up. A sign-in that started from the saved sign-in, in a window that loaded it, is kept already;
    one switched from it to another tenant cannot be kept, as the error explains.
 
    .PARAMETER PassThru
    Write the context (VirtualMetric.Context) to the pipeline.
 
    .EXAMPLE
    Save-VMetricContext
 
    Keeps the current sign-in for new windows.
 
    .EXAMPLE
    Connect-VMetric -Uri https://console.example.com -Tenant Contoso; Save-VMetricContext -PassThru
 
    Signs in to the Contoso tenant, keeps that sign-in for new windows, and shows the context.
 
    .OUTPUTS
    VirtualMetric.Context, with -PassThru.
 
    .LINK
    Connect-VMetric
 
    .LINK
    Disconnect-VMetric
 
    .LINK
    Get-VMetricContext
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
    [OutputType('VirtualMetric.Context')]
    param(
        [switch] $PassThru
    )

    if (-not $PSBoundParameters.ContainsKey('InformationAction')) {
        $InformationPreference = 'Continue'
    }

    try {
        $session = Get-VMetricSession
        $tenantText = if ($session.TenantName) { " ($(ConvertTo-VMetricSafeText -Text ([string]$session.TenantName)))" } else { '' }
        # The marker, not this window's memory: another window may have signed it out or saved another.
        if (Test-VMetricSessionPersisted -Session $session) {
            Write-Information "The sign-in to $($session.ConsoleUrl)$tenantText is the one kept for new windows already."
        }
        elseif ($PSCmdlet.ShouldProcess("$($session.ConsoleUrl)$tenantText", 'Keep the sign-in for new PowerShell windows')) {
            $vault = Save-VMetricSavedContext -Session $session
            Write-Information "Kept for new PowerShell windows in vault '$(ConvertTo-VMetricSafeText -Text $vault)'; Disconnect-VMetric signs it out."
        }
        if ($PassThru) {
            ConvertTo-VMetricContextObject -Session $session
        }
    }
    catch {
        if (Test-VMetricFlowControl -ErrorRecord $_) {
            throw
        }
        Write-VMetricCmdletError -Cmdlet $PSCmdlet -ErrorRecord $_ -Terminating
    }
}