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..dec63bb 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 |