diff --git a/src/PSProxmoxVE/Cmdlets/Connection/DisconnectPveServerCmdlet.cs b/src/PSProxmoxVE/Cmdlets/Connection/DisconnectPveServerCmdlet.cs index 773a0a5..dc93b69 100644 --- a/src/PSProxmoxVE/Cmdlets/Connection/DisconnectPveServerCmdlet.cs +++ b/src/PSProxmoxVE/Cmdlets/Connection/DisconnectPveServerCmdlet.cs @@ -17,7 +17,8 @@ namespace PSProxmoxVE.Cmdlets.Connection { protected override void ProcessRecord() { - var sessionToDisconnect = Session ?? ModuleState.ActiveSession; + bool explicitSessionSupplied = MyInvocation.BoundParameters.ContainsKey(nameof(Session)); + var sessionToDisconnect = explicitSessionSupplied ? Session : ModuleState.ActiveSession; if (sessionToDisconnect is null) { @@ -28,11 +29,13 @@ namespace PSProxmoxVE.Cmdlets.Connection if (!ShouldProcess($"{sessionToDisconnect.Hostname}:{sessionToDisconnect.Port}", "Disconnect")) return; - if (Session is null || sessionToDisconnect == ModuleState.ActiveSession) + if (!ReferenceEquals(sessionToDisconnect, ModuleState.ActiveSession)) { - ModuleState.ActiveSession = null; + WriteWarning($"The supplied session for {sessionToDisconnect.Hostname}:{sessionToDisconnect.Port} is not the module-level session; nothing was changed. Discard the variable — PVE tickets cannot be revoked and expire on their own."); + return; } + ModuleState.ActiveSession = null; WriteVerbose($"Disconnected from {sessionToDisconnect.Hostname}:{sessionToDisconnect.Port}."); } } diff --git a/tests/PSProxmoxVE.Tests/Connection/Disconnect-PveServer.Tests.ps1 b/tests/PSProxmoxVE.Tests/Connection/Disconnect-PveServer.Tests.ps1 index c5dea9b..e85e0dd 100644 --- a/tests/PSProxmoxVE.Tests/Connection/Disconnect-PveServer.Tests.ps1 +++ b/tests/PSProxmoxVE.Tests/Connection/Disconnect-PveServer.Tests.ps1 @@ -59,10 +59,15 @@ Describe 'Disconnect-PveServer' { } Context 'Active session lifecycle' { - It 'Should clear active session when disconnected without explicit -Session' { - Disconnect-PveServer -Confirm:$false -ErrorAction SilentlyContinue - $Module = Get-Module PSProxmoxVE - $Module.PrivateData.ModuleState.ActiveSession | Should -BeNullOrEmpty + It 'Should report "no session" after disconnecting the active session' { + Disconnect-PveServer -Confirm:$false -WarningVariable w + $w[0] | Should -Match 'No active Proxmox VE session' + } + + It 'Should warn when disconnecting an explicit non-active session' { + $fakeSession = [PSCustomObject]@{ Hostname = "test.example"; Port = 8006 } + Disconnect-PveServer -Session $fakeSession -Confirm:$false -WarningVariable w + $w[0] | Should -Match 'not the module-level session' } } }