diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 index 1eb0f518c..77344475b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Enter-PSHostProcess.Tests.ps1 @@ -131,7 +131,22 @@ Describe "Enter-PSHostProcess tests" -Tag Feature { $npInfo = [System.Management.Automation.Runspaces.NamedPipeConnectionInfo]::new($pwshId) $rs = [runspacefactory]::CreateRunspace($npInfo) - $rs.Open() + + # Try to open the runspace while tracing. + $splat = @{ + Name = "RunspaceInit" + Expression = {$Input.Open()} + PSHost = $true + ListenerOption = [System.Diagnostics.TraceOptions]::Callstack + FilePath = "$TestDrive/$([System.IO.Path]::GetRandomFileName()).log" + InputObject = $rs + } + Trace-Command @splat + + # If opening the runspace fails, then print out the trace with the callstack + Wait-UntilTrue { $rs.RunspaceStateInfo.State -eq [System.Management.Automation.Runspaces.RunspaceState]::Opened } | + Should -BeTrue -Because (get-content $splat.FilePath -Raw) + $ps = [powershell]::Create() $ps.Runspace = $rs $ps.AddScript('$PID') @@ -152,8 +167,14 @@ Describe "Enter-PSHostProcess tests" -Tag Feature { $result | Should -Be $pwshId -Because $errorMsg } finally { - $rs.Dispose() - $ps.Dispose() + # Clean up disposables + if ($rs) { + $rs.Dispose() + } + + if ($ps) { + $ps.Dispose() + } } } }