Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions LazyWinAdminModule/Tests/Get-LocalOrRemoteCimSession.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" }
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Loading