Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -811,33 +811,18 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
$ellipsis = ""`u{2026}""
$resetColor = ''
$errorColor = ''
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
if ($null -ne $psstyle) {
$errorColor = $psstyle.Formatting.Error
}

$resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence(
[System.Management.Automation.VTUtility+VT]::Reset
)
}
$accentColor = ''

function Get-VT100Color([ConsoleColor] $color) {
if (!$Host.UI.SupportsVirtualTerminal -or !([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
return ''
}

return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
$resetColor = $PSStyle.Reset
$errorColor = $psstyle.Formatting.Error
$accentColor = $PSStyle.Formatting.FormatAccent
}

function Show-ErrorRecord($obj, [int]$indent = 0, [int]$depth = 1) {
$newline = [Environment]::Newline
$output = [System.Text.StringBuilder]::new()
$prefix = ' ' * $indent
$accentColor = ''

if ($null -ne $Host.PrivateData) {
$accentColor = Get-VT100Color ($Host.PrivateData.FormatAccentColor ?? $Host.PrivateData.ErrorForegroundColor)
}

$expandTypes = @(
'Microsoft.Rest.HttpRequestMessageWrapper'
Expand Down Expand Up @@ -1066,36 +1051,17 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
Set-StrictMode -Off
$newline = [Environment]::Newline

function Get-ConciseViewPositionMessage {

$resetColor = ''
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
$resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence(
[System.Management.Automation.VTUtility+VT]::Reset
)
}

function Get-VT100Color([ConsoleColor] $color) {
if (!$Host.UI.SupportsVirtualTerminal -or !([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
return ''
}

return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
}
$resetColor = ''
$errorColor = ''
$accentColor = ''

# return length of string sans VT100 codes
function Get-RawStringLength($string) {
$vtCodes = ""`e[0m"", ""`e[2;30m"", ""`e[2;31m"", ""`e[2;32m"", ""`e[2;33m"", ""`e[2;34m"",
""`e[2;35m"", ""`e[2;36m"", ""`e[2;37m"", ""`e[1;30m"", ""`e[1;31m"", ""`e[1;32m"",
""`e[1;33m"", ""`e[1;34m"", ""`e[1;35m"", ""`e[1;36m"", ""`e[1;37m""

$newString = $string
foreach ($vtCode in $vtCodes) {
$newString = $newString.Replace($vtCode, '')
}
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
$resetColor = $PSStyle.Reset
$errorColor = $PSStyle.Formatting.Error
$accentColor = $PSStyle.Formatting.ErrorAccent
}

return $newString.Length
}
function Get-ConciseViewPositionMessage {

# returns a string cut to last whitespace
function Get-TruncatedString($string, [int]$length) {
Expand All @@ -1107,14 +1073,6 @@ function Get-ConciseViewPositionMessage {
return ($string.Substring(0,$length) -split '\s',-2)[0]
}

$errorColor = ''
$accentColor = ''

if ($null -ne $Host.PrivateData) {
$errorColor = Get-VT100Color $Host.PrivateData.ErrorForegroundColor
$accentColor = Get-VT100Color ($Host.PrivateData.ErrorAccentColor ?? $errorColor)
}

$posmsg = ''
$headerWhitespace = ''
$offsetWhitespace = ''
Expand Down Expand Up @@ -1217,7 +1175,7 @@ function Get-ConciseViewPositionMessage {

# if rendering line information, break up the message if it's wider than the console
if ($myinv -and $myinv.ScriptName -or $err.CategoryInfo.Category -eq 'ParserError') {
$prefixLength = Get-RawStringLength -string $prefix
$prefixLength = [System.Management.Automation.Internal.StringDecorated]::new($prefix).ContentLength
$prefixVtLength = $prefix.Length - $prefixLength

# replace newlines in message so it lines up correct
Expand Down Expand Up @@ -1296,82 +1254,77 @@ function Get-ConciseViewPositionMessage {
$myinv = $err.InvocationInfo
}

if ($ErrorView -ne 'ConciseView' -and $null -ne $host.PrivateData) {
if ($null -ne $host.PrivateData.ErrorForegroundColor) {
[Console]::ForegroundColor = $host.PrivateData.ErrorForegroundColor
}
if ($err.FullyQualifiedErrorId -eq 'NativeCommandErrorMessage' -or $err.FullyQualifiedErrorId -eq 'NativeCommandError') {
return ""${errorColor}$($err.Exception.Message)${resetcolor}""
}

if ($null -ne $host.PrivateData.ErrorBackgroundColor) {
[Console]::BackgroundColor = $host.PrivateData.ErrorBackgroundColor
}
$myinv = $err.InvocationInfo
if ($ErrorView -eq 'DetailedView') {
$message = Get-Error | Out-String
return ""${errorColor}${message}${resetcolor}""
}

if ($err.FullyQualifiedErrorId -eq 'NativeCommandErrorMessage' -or $err.FullyQualifiedErrorId -eq 'NativeCommandError') {
$err.Exception.Message
if ($ErrorView -eq 'CategoryView') {
$message = $err.CategoryInfo.GetMessage()
return ""${errorColor}${message}${resetcolor}""
}
else
{
$myinv = $err.InvocationInfo
if ($ErrorView -eq 'DetailedView') {
return (Get-Error | Out-String)
}
elseif ($ErrorView -eq 'ConciseView') {
$posmsg = Get-ConciseViewPositionMessage
}
elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) {
$posmsg = $myinv.PositionMessage
} else {
$posmsg = ''
}

if ($posmsg -ne '')
{
$posmsg = $newline + $posmsg
}
$posmsg = ''
if ($ErrorView -eq 'ConciseView') {
$posmsg = Get-ConciseViewPositionMessage
}
elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) {
$posmsg = $myinv.PositionMessage
}

if ($err.PSMessageDetails) {
$posmsg = ' : ' + $err.PSMessageDetails + $posmsg
}
if ($posmsg -ne '') {
$posmsg = $newline + $posmsg
}

if ($err.PSMessageDetails) {
$posmsg = ' : ' + $err.PSMessageDetails + $posmsg
}

if ($ErrorView -eq 'ConciseView') {
return $posmsg
if ($ErrorView -eq 'ConciseView') {
if ($err.PSMessageDetails) {
$posmsg = ""${errorColor}${posmsg}""
}
return $posmsg
}

$indent = 4
$indent = 4

$errorCategoryMsg = $err.ErrorCategory_Message
$errorCategoryMsg = $err.ErrorCategory_Message

if ($null -ne $errorCategoryMsg)
{
$indentString = '+ CategoryInfo : ' + $err.ErrorCategory_Message
}
else
{
$indentString = '+ CategoryInfo : ' + $err.CategoryInfo
}
if ($null -ne $errorCategoryMsg)
{
$indentString = '+ CategoryInfo : ' + $err.ErrorCategory_Message
}
else
{
$indentString = '+ CategoryInfo : ' + $err.CategoryInfo
}

$posmsg += $newline + $indentString
$posmsg += $newline + $indentString

$indentString = ""+ FullyQualifiedErrorId : "" + $err.FullyQualifiedErrorId
$posmsg += $newline + $indentString
$indentString = ""+ FullyQualifiedErrorId : "" + $err.FullyQualifiedErrorId
$posmsg += $newline + $indentString

$originInfo = $err.OriginInfo
$originInfo = $err.OriginInfo

if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName))
{
$indentString = ""+ PSComputerName : "" + $originInfo.PSComputerName
$posmsg += $newline + $indentString
}
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName))
{
$indentString = ""+ PSComputerName : "" + $originInfo.PSComputerName
$posmsg += $newline + $indentString
}

if ($ErrorView -eq 'CategoryView') {
$err.CategoryInfo.GetMessage()
}
elseif (! $err.ErrorDetails -or ! $err.ErrorDetails.Message) {
$err.Exception.Message + $posmsg
} else {
$err.ErrorDetails.Message + $posmsg
}
$finalMsg = if ($err.ErrorDetails.Message) {
$err.ErrorDetails.Message + $posmsg
} else {
$err.Exception.Message + $posmsg
}

""${errorColor}${finalMsg}${resetcolor}""
")
.EndEntry()
.EndControl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4335,7 +4335,7 @@ static InitialSessionState()
SpecialVariables.PSStyle,
PSStyle.Instance,
RunspaceInit.PSStyleDescription,
ScopedItemOptions.None),
ScopedItemOptions.Constant),

// Variable which controls the encoding for piping data to a NativeCommand
new SessionStateVariableEntry(
Expand Down
5 changes: 3 additions & 2 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,15 @@ export $envVarName='$guid'
It "errors are in text if error is redirected, encoded command, non-interactive, and outputformat specified" {
$p = [Diagnostics.Process]::new()
$p.StartInfo.FileName = "pwsh"
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('$ErrorView="NormalView";throw "boom"'))
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('throw "boom"'))
$p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardError = $true
$p.Start() | Out-Null
$out = $p.StandardError.ReadToEnd()
$out | Should -Not -BeNullOrEmpty
$out.Split([Environment]::NewLine)[0] | Should -BeExactly "boom"
$out = $out.Split([Environment]::NewLine)[0]
[System.Management.Automation.Internal.StringDecorated]::new($out).ToString("PlainText") | Should -BeExactly "Exception: boom"
}
}

Expand Down