From b35bbb71dcbc32f268793245a85a548d783bd46b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 5 Jan 2023 20:09:24 -0800 Subject: [PATCH 1/2] Fix `Get-Error` to work with strict mode --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 10 ++++------ .../Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 549a4e6d4b7..681a6221935 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1016,14 +1016,15 @@ private static IEnumerable ViewsOf_System_Management_Autom CustomControl.Create(outOfBand: true) .StartEntry() .AddScriptBlockExpressionBinding(@" + $errorColor = '' + $commandPrefix = '' if (@('NativeCommandErrorMessage','NativeCommandError') -notcontains $_.FullyQualifiedErrorId -and @('CategoryView','ConciseView','DetailedView') -notcontains $ErrorView) { $myinv = $_.InvocationInfo - $errorColor = '' if ($Host.UI.SupportsVirtualTerminal) { $errorColor = $PSStyle.Formatting.Error } - + $commandPrefix = if ($myinv -and $myinv.MyCommand) { switch -regex ( $myinv.MyCommand.CommandType ) { @@ -1070,10 +1071,7 @@ private static IEnumerable ViewsOf_System_Management_Autom } } - if ($commandPrefix) - { - $errorColor + $commandPrefix - } + $errorColor + $commandPrefix ") .AddScriptBlockExpressionBinding(@" Set-StrictMode -Off diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 index 624649fdfc4..1d89bf6c1fa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 @@ -145,4 +145,9 @@ Describe 'Get-Error tests' -Tag CI { } } } + + It 'Get-Error works with strict mode' { + $out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String + $out | Should -Match "Message : Attempted to divide by zero." + } } From cef101c8afba94bd4fbe5552bfc94f67153217bb Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 5 Jan 2023 22:11:32 -0800 Subject: [PATCH 2/2] remove extra whitespace --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 681a6221935..c490f9e7a64 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1024,7 +1024,7 @@ private static IEnumerable ViewsOf_System_Management_Autom if ($Host.UI.SupportsVirtualTerminal) { $errorColor = $PSStyle.Formatting.Error } - + $commandPrefix = if ($myinv -and $myinv.MyCommand) { switch -regex ( $myinv.MyCommand.CommandType ) {