diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs index bf907203544..40c4ec63568 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs @@ -214,7 +214,7 @@ public class WriteOrThrowErrorCommand : PSCmdlet /// /// ErrorRecord.Exception -- if not specified, ErrorRecord.Exception is System.Exception. /// - [Parameter(ParameterSetName = "WithException", Mandatory = true)] + [Parameter(Position = 0, ParameterSetName = "WithException", Mandatory = true)] public Exception Exception { get; set; } /// @@ -232,9 +232,9 @@ public class WriteOrThrowErrorCommand : PSCmdlet /// If Exception is specified, this is ErrorRecord.ErrorDetails.Message; /// otherwise, the Exception is System.Exception, and this is Exception.Message. /// - [Parameter(ParameterSetName = "ErrorRecord", Mandatory = true)] + [Parameter(Position = 0, ParameterSetName = "ErrorRecord", Mandatory = true)] public ErrorRecord ErrorRecord { get; set; } - + /// /// ErrorRecord.CategoryInfo.Category. /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Error.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Error.Tests.ps1 index ce52a59285a..d243ec07988 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Error.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Error.Tests.ps1 @@ -105,4 +105,31 @@ Describe "Write-Error Tests" -Tags "CI" { $result.Count | Should -BeExactly 3 $result[0] | Should -Match $longtext } + + It "Should be able to pass ErrorRecord to parameter position 0." { + try { + [int]::parse('foo') + } catch { } + + (Write-Error $Error[0] 2>&1).Exception.GetType().FullName | + Should -Be System.Management.Automation.MethodInvocationException + } + + It "Should be able to pass Exception to parameter position 0." { + try { + [int]::parse('foo') + } catch { } + + (Write-Error $Error[0].Exception.InnerException 2>&1).Exception.GetType().FullName | + Should -Be System.FormatException + } + + It "Should be able to pass string to parameter position 0." { + try { + [int]::parse('foo') + } catch { } + + (Write-Error "$($Error[0])" 2>&1).Exception.GetType().FullName | + Should -Be Microsoft.PowerShell.Commands.WriteErrorException + } }