Skip to content

Transcription of Write-Information works if preference is 'SilentlyContinue' #6916

Closed

Description

Related issue: #4645

run.ps1

Start-Transcript -Path (Join-Path $PSScriptRoot 'transcriptdata.txt')
$InformationPreference = 'SilentlyContinue'
Write-Information 'SilentlyContinue'
$InformationPreference = 'Continue'
Write-Information 'LoudlyContinue'
Stop-Transcript

Steps to reproduce

.\run.ps1

Expected behavior

transcriptdata.txt should contains only LoudlyContinue

Actual behavior

transcriptdata.txt contains only SilentlyContinue

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0-preview.1
PSEdition                      Core
GitCommitId                    v6.1.0-preview.1
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Investigation

The problem is in if/else statement which starts here:

if ((record.Tags.Contains("PSHOST") && (!record.Tags.Contains("FORWARDED")))
|| (preference == ActionPreference.Continue))

and has a continuation here:
else
{
// Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods.
CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString()));
}

Transcription of Write-Host commands are handled in InternalUI.Write* methods as the comment suggests. These commands are distinguished by "PSHOST" tag.
Transcription of Write-Information commands is supposed to take place in the else statement but code in this statement is being executed only if

(!record.Tags.Contains("PSHOST") || record.Tags.Contains("FORWARDED"))
&&
preference != ActionPreference.Continue)

This condition is invalid. Correct condition is:

!record.Tags.Contains("PSHOST")
&&
preference == ActionPreference.Continue)

PR pending...

Activity

  1. iSazonov commented on May 22, 2018

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Could you please comment?

  2. mklement0 commented on May 22, 2018

    @mklement0
    Contributor

    Thanks for taking this on, Hubert Bukowski (@hubuk).

    It's definitely a step in the right direction, but other quirks remain, so maybe we can expand the scope of your PR.

    The following test code contrast the console output of various Write-Information / Write-Host / Out-Host commands with respect to what prints to the console vs. what shows in the transcript (which should be the same):

    "--- CONSOLE output`n"
    $null = Start-Transcript
    
    Write-Host 'write-host silentlycontinue'
    Write-Information 'write-info silentlycontinue'
    
    Write-Host 'write-host continue' -InformationAction Continue
    Write-Information 'write-info continue' -InformationAction Continue
    
    Write-Host 'write-host ignore' -InformationAction Ignore
    Write-Information 'write-info ignore' -InformationAction Ignore
    
    Out-Host -InputObject 'out-host' # always prints to both the console and the transcript
    
    
    $f = (Stop-Transcript).Path
    "`n---- TRANSCRIPT contents"
    ((gc -raw $f) -split '(?m)^\*+$')[2]
    '----'
    Remove-Item $f
    • Your current PR commendably fixes the Write-Information behavior in principle, but in the transcript its output is still prefixed with INFO: , unlike in the console.

    • Write-Host output still invariably end up in the transcript - while -InformationAction Ignore / 6>$null works in the console, it doesn't in the transcript.

    While fixing the quirks is definitely desirable, we also have to ask ourselves whether user may depend on the old behavior, such as expecting Write-Information output to be there by default.

    Steve Lee (@SteveL-MSFT), any thoughts on this and the other issues mentioned in #4645 with respect to backward compatibility?

  3. iSazonov commented on May 23, 2018

    @iSazonov
    Collaborator

    I agree with fixing the Issue too.

    For others I'd open new issue for every inconsistency - I believe we get conclusions faster.

  4. hubuk commented on May 23, 2018

    @hubuk
    ContributorAuthor

    Michael Klement (@mklement0) I made changes to INFO: prefix, but I observe different behavior in Write-Host 'write-host ignore' -InformationAction Ignore than you described. It is present in transcript but not in the console.

    --- CONSOLE output
    write-host silentlycontinue
    write-host continue
    write-info continue
    out-host
    ---- TRANSCRIPT contents
    write-host silentlycontinue
    write-host continue
    write-info continue
    write-host ignore
    out-host
    ----
    

    I think that Write-Host should always be visible in both console and transcript regardless of InformationAction and $InformationPreference.

  5. added
    WG-Engine
    Issue-Discussion
    on May 23, 2018
  6. SteveL-MSFT commented on May 23, 2018

    @SteveL-MSFT
    Member

    My general view is that Start-Transcript, by default, is expected to log your inputs and the outputs and should resemble what you would see at the console. Anything more than that should require the use of a parameter or switch. Where behavior changes (breaking changes) are needed to accomplish this, we'll need to determine the impact to the user and decide on a case-by-base basis.

  7. mklement0 commented on May 23, 2018

    @mklement0
    Contributor

    Hubert Bukowski (@hubuk):

    but I observe different behavior in Write-Host 'write-host ignore' -InformationAction Ignore than you described. It is present in transcript but not in the console.

    Yes, that is the behavior I was trying to describe.

    I think that Write-Host should always be visible in both console and transcript regardless of InformationAction and $InformationPreference.

    That applies to Out-Host, but not to Write-Host, which is now a bit of a misnomer in that it writes to the information stream, not to the host directly.

    From what I understand it is by design that in v5+ you can silence Write-Host with -InformationAction Ignore / 6>$null (and that you can generally redirect it with 6>).

    In short: My understanding is that Write-Host is now an output-by-default companion to the silent-by-default Write-Information (with additional, UI-related features).

  8. iSazonov commented on May 23, 2018

    @iSazonov
    Collaborator

    Write-Host, which is now a bit of a misnomer in that it writes to the information stream, not to the host directly.

    It writes to the informational stream and then to host directly based on tag presence.

  9. mklement0 commented on May 23, 2018

    @mklement0
    Contributor

    Ilya (@iSazonov): Yes, but you can suppress Write-Host output to the host with -InformationAction Ignore / 6>$null, whereas the output still shows in the transcript, and that's the problem.

  10. iSazonov commented on May 23, 2018

    @iSazonov
    Collaborator

    the output still shows in the transcript, and that's the problem

    Agree.

  11. hubuk commented on May 23, 2018

    @hubuk
    ContributorAuthor

    the output still shows in the transcript, and that's the problem

    If -InformatioAction Ignore should suppress Write-Host from writing to console then -InformationAction SilentlyContinue should also suppress console output for consistency.

    But for me it seems more natural that Write-Host should always writes to host. Its interaction with InformationStream can be controlled using InformationAction and InformationPreference variable. If the appearance on the console shall be controlled using these then Write-Information should be used instead. That was my understanding of the differences between these two commands.

  12. mklement0 commented on May 23, 2018

    @mklement0
    Contributor

    If -InformationAction Ignore should suppress Write-Host from writing to console, then -InformationAction SilentlyContinue should also suppress console output for consistency.

    That's not possible, because that would make Write-Host silent by default, given that $InformationPreference defaults to SilentlyContinue and, in the absence of an explicit -InformationAction argument, is equivalent to passing -InformationAction SilentlyContinue.

    For backward compatibility (alone) you don't want Write-Host to be silent by default.

    But for me it seems more natural that Write-Host should always writes to host.

    That's what the name of the cmdlet suggests, and that's how it used to work.

    However, as https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Utility/Write-Information.md states:

    Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information. You can now use Write-Host to emit output to the information stream, but the $InformationPreference preference variable and InformationAction common parameter do not affect Write-Host messages.

    (Ignore the fact that this is not the entirely accurate, because the Ignore value passed to -InformationAction does affect Write-Host and is in effect equivalent to 6>$null.)

    Presumably - and perhaps someone from the team can confirm - this slight repurposing of Write-Host since v5 was to compensate for frequent misuse of Write-Host by cmdlet/function authors, which resulted in callers being unable to capture or suppress its output.

    By making Write-Host write to the information stream (number 6) instead, it became possible to either suppress or capture its output. (Note that, as stated, Out-Host indeed still only and invariably prints to the host (and possibly transcript)).

  13. hubuk commented on Jun 16, 2018

    @hubuk
    ContributorAuthor

    Closed via #6917.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-FixedWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions