-
I have a Mock that is successful in PowerShell 5 (Pester v5.4.0) but fails in PowerShell 7 (Pester v5.4.0) as the catch is handled differently. PowerShell 5.1.190.41 As part of the logic in the function being Mocked, it throws an error message e.g. "More than one secret present". There are two catch statements, one that catches a specific exception type i.e. What I find is that in PowerShell 5, when the error message "More than one secret is present" the first catch statement that catches the exception type What I find with PowerShell 7, when the error message "More than one secret is present" is thrown, the first catch statement isn't skipped as it does in PowerShell 5 and it errors with the message Is anyone able to explain why this might be the case please. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi. I can't really confirm without a repro, but I'm guessing this is caused by Assembly Load Context (ALC). ALCs are a feature in .NET only available for PowerShell 7+ meant to help avoid version conflicts for assemblies etc. So the module creating the exception is likely using that to avoid conflicts with other modules depending on a different version of the same dlls in the Azure SDK. AFAIK using ALC hides types like the exception unless explicitly exposed, so you (and Pester) can't reference the type As a workaround you might be able to match the exception type-name in the generic catch-clause. Ex: try {
throw [System.IO.EndOfStreamException]::new("hello")
} catch {
if ($_.Exception.GetType().FullName -eq 'System.IO.EndOfStreamException') {
'caught you'
} else { 'what is this?' }
} |
Beta Was this translation helpful? Give feedback.
I don't think Pester is relevant here. PowerShell is unable to find the type, so it can't execute your try/catch statement at all regardless of which clause it should match.
Ex. run this directly:
As mentioned, ALC is only available in PowerShell 7+, so when you use the same modules …