Skip to content

Commit 2baeb1a

Browse files
authored
Fix Get-Error to work with strict mode (PowerShell#18895)
1 parent f018bee commit 2baeb1a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,11 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
10161016
CustomControl.Create(outOfBand: true)
10171017
.StartEntry()
10181018
.AddScriptBlockExpressionBinding(@"
1019+
$errorColor = ''
1020+
$commandPrefix = ''
10191021
if (@('NativeCommandErrorMessage','NativeCommandError') -notcontains $_.FullyQualifiedErrorId -and @('CategoryView','ConciseView','DetailedView') -notcontains $ErrorView)
10201022
{
10211023
$myinv = $_.InvocationInfo
1022-
$errorColor = ''
10231024
if ($Host.UI.SupportsVirtualTerminal) {
10241025
$errorColor = $PSStyle.Formatting.Error
10251026
}
@@ -1070,10 +1071,7 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
10701071
}
10711072
}
10721073
1073-
if ($commandPrefix)
1074-
{
1075-
$errorColor + $commandPrefix
1076-
}
1074+
$errorColor + $commandPrefix
10771075
")
10781076
.AddScriptBlockExpressionBinding(@"
10791077
Set-StrictMode -Off

test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,9 @@ Describe 'Get-Error tests' -Tag CI {
145145
}
146146
}
147147
}
148+
149+
It 'Get-Error works with strict mode' {
150+
$out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String
151+
$out | Should -Match "Message : Attempted to divide by zero."
152+
}
148153
}

0 commit comments

Comments
 (0)