From 760e26592340d0c799d781aae775e5c6d92a6844 Mon Sep 17 00:00:00 2001 From: dutch2005 Date: Wed, 23 Sep 2026 22:30:01 +0200 Subject: [PATCH 1/2] test(cim): use real session for reuse regression (#15) --- .../Tests/Get-LocalOrRemoteCimSession.Tests.ps1 | 17 +++++++++++++---- README.md | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/LazyWinAdminModule/Tests/Get-LocalOrRemoteCimSession.Tests.ps1 b/LazyWinAdminModule/Tests/Get-LocalOrRemoteCimSession.Tests.ps1 index 8e86975..5d18841 100644 --- a/LazyWinAdminModule/Tests/Get-LocalOrRemoteCimSession.Tests.ps1 +++ b/LazyWinAdminModule/Tests/Get-LocalOrRemoteCimSession.Tests.ps1 @@ -4,9 +4,17 @@ BeforeAll { Describe "Get-LocalOrRemoteCimSession" { It "reuses an existing CimSession if passed via -ReuseSession" { - $mockSession = [Microsoft.Management.Infrastructure.CimSession]::new() - $result = Get-LocalOrRemoteCimSession -ComputerName "Server1" -ReuseSession $mockSession - $result | Should -Be $mockSession + $remoteName = "Server1" + $existingSession = New-CimSession -ErrorAction Stop + try { + Mock New-CimSession { throw 'A reused session must not be recreated' } + $result = Get-LocalOrRemoteCimSession -ComputerName $remoteName -ReuseSession $existingSession + $result | Should -Be $existingSession + Should -Invoke New-CimSession -Times 0 + } + finally { + Remove-CimSession -CimSession $existingSession + } } It "creates a local CimSession when ComputerName is localhost" { @@ -17,8 +25,9 @@ Describe "Get-LocalOrRemoteCimSession" { } It "creates a remote CimSession when ComputerName is a remote server" { + $remoteName = "Server1" Mock New-CimSession { return "RemoteSession" } -ParameterFilter { $ComputerName -eq "Server1" } - $result = Get-LocalOrRemoteCimSession -ComputerName "Server1" + $result = Get-LocalOrRemoteCimSession -ComputerName $remoteName $result | Should -Be "RemoteSession" Assert-MockCalled New-CimSession -Times 1 -ParameterFilter { $ComputerName -eq "Server1" } } diff --git a/README.md b/README.md index 696c10f..f5210b0 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,9 @@ All button actions run in background thread jobs via `Start-ThreadJob`. Complete In PowerShell 7+, `Get-CimInstance -ComputerName localhost` routes through WSMan (WinRM) even for the local machine. If WinRM is not running, CIM jobs stall for the full connection timeout (30 s), saturate the `Start-ThreadJob` pool (5 slots), and freeze the UI. All CIM-based private functions now detect a local target and omit `-ComputerName` entirely, using a direct in-process CIM session instead. +### CIM session reuse + +Private CIM actions use Get-LocalOrRemoteCimSession to select the local DCOM path or a named remote target. Callers may pass ReuseSession to retain an existing session, and actions that create a session dispose it when finished. The dedicated Pester suite covers local, remote, and reuse behavior. ### Pre-flight guards | Guard | Blocks | From 176773317b9901f16c0dde83b789a22701757ce8 Mon Sep 17 00:00:00 2001 From: dutch2005 Date: Wed, 23 Sep 2026 22:30:22 +0200 Subject: [PATCH 2/2] docs(cim): format session reuse guidance --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5210b0..dec63bb 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ In PowerShell 7+, `Get-CimInstance -ComputerName localhost` routes through WSMan ### CIM session reuse -Private CIM actions use Get-LocalOrRemoteCimSession to select the local DCOM path or a named remote target. Callers may pass ReuseSession to retain an existing session, and actions that create a session dispose it when finished. The dedicated Pester suite covers local, remote, and reuse behavior. +Private CIM actions use `Get-LocalOrRemoteCimSession` to select the local DCOM path or a named remote target. Callers may pass `-ReuseSession` to retain an existing session, and actions that create a session dispose it when finished. The dedicated Pester suite covers local, remote, and reuse behavior. ### Pre-flight guards | Guard | Blocks |