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 a4b3127a454..e93275ab70e 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1104,6 +1104,15 @@ function Get-ConciseViewPositionMessage { if ($_.Exception -and $_.Exception.WasThrownFromThrowStatement) { $reason = 'Exception' } + # MyCommand can be the script block, so we don't want to show that so check if it's an actual command + elseif ($myinv.MyCommand -and (Get-Command -Name $myinv.MyCommand -ErrorAction Ignore)) + { + $reason = $myinv.MyCommand + } + # If it's a scriptblock, better to show the command in the scriptblock that had the error + elseif ($_.CategoryInfo.Activity) { + $reason = $_.CategoryInfo.Activity + } elseif ($myinv.MyCommand) { $reason = $myinv.MyCommand } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 index 19c23d90e6f..8f61a921082 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 @@ -30,7 +30,7 @@ Describe "Stream writer tests" -Tags "CI" { It "Should write error messages to the error stream" { Write-Error "Testing Error" 2>&1 > $targetfile # The contents of the error stream should contain the expected text - $targetfile | Should -FileContentMatch ": Testing Error" + $targetfile | Should -FileContentMatch "Testing Error" } It "Should write debug messages to the debug stream" { diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index 2865757f072..b1ffdfc05f9 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -45,5 +45,17 @@ Describe 'Tests for $ErrorView' -Tag CI { Start-Job -ScriptBlock { get-item (new-guid) } | Wait-Job | Receive-Job -ErrorVariable e -ErrorAction SilentlyContinue ($e | Out-String).Trim().Count | Should -Be 1 } + + It "Activity shows up correctly for scriptblocks" { + $e = pwsh -noprofile -command 'Write-Error 'myError' -ErrorAction SilentlyContinue; $error[0] | Out-String' + [string]::Join('', $e).Trim() | Should -BeLike "*Write-Error:*myError*" # wildcard due to VT100 + } + + It "Function shows up correctly" { + function test-myerror { [cmdletbinding()] param() write-error 'myError' } + + $e = pwsh -noprofile -command 'function test-myerror { [cmdletbinding()] param() write-error "myError" }; test-myerror -ErrorAction SilentlyContinue; $error[0] | Out-String' + [string]::Join('', $e).Trim() | Should -BeLike "*test-myerror:*myError*" # wildcard due to VT100 + } } }