PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
45.14k stars 7.28k forks source link

Inconsistent display of data #12154

Closed zammitt closed 4 years ago

zammitt commented 4 years ago

I want to capture all data emmitted from a command to display to the user at a later time.

Using the common -*Varaible parameters captures everything in the correct order, however when I display them later the formatting compared to what the console originally printed is lost.

Steps to reproduce

function Get-DataTest {
    [CmdletBinding()]
    param()
    Write-Host "Hello from Write-Host" -ForegroundColor Green
    Write-Warning "Hello from Write-Warning"
    Write-Output "Hello from Write-Output"
    Write-Error "Hello from Write-Error"
}

$buffer = $null

Write-Host "`nInvoking Get-DataTest"

Get-DataTest -InformationVariable +buffer -WarningVariable +buffer -OutVariable +buffer -ErrorVariable +buffer -PipelineVariable +buffer

Write-Host "`nPrinting buffer"

$buffer

Expected behavior

Printing $buffer should match the output from Get-DataTest.

Actual behavior

Color from Write-Host and Write-Warning is lost. Warning: label for Write-Warning is lost. Surprisingly, the data captured from Write-Error is displaying with color and in the exact same format.

image

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
SteveL-MSFT commented 4 years ago

This is because that coloring is actually applied by the host (consolehost in this case) whereas the error formatting is VT100 escape sequences embedded in the text. You can use $host.PrivateData to get the colors that would have been used.

SeeminglyScience commented 4 years ago

@SteveL-MSFT Warning is still kind of inconsistent though. I'd expect that a WarningRecord would be formatted similar to how it's stream appears. I know that's host specific, but I don't think it would necessarily be a bad idea to give it formatting similar to how ConsoleHost emits it.

(To clarify, objects saved to WarningVariable are actually typed as WarningRecord instead of just a plain string)

ghost commented 4 years ago

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.