Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success stream not consistent with other streams #14464

Closed
metablaster opened this issue Dec 19, 2020 · 2 comments
Closed

Success stream not consistent with other streams #14464

metablaster opened this issue Dec 19, 2020 · 2 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.

Comments

@metablaster
Copy link

metablaster commented Dec 19, 2020

Steps to reproduce

function Test-Stream
{
	foreach ($i in 1..3)
	{
		Write-Information -MessageData "Performing operation $i" -INFA "Continue"

		[PSCustomObject]@{
			Key1 = "A$i"
			Key2 = "B$i"
		}
	}
}

Test-Stream

Expected behavior

Performing operation 1

Key1 Key2
---- ----
A1   B1
Performing operation 2
A2   B2
Performing operation 3
A3   B3

Actual behavior

Performing operation 1

Performing operation 2
Performing operation 3
Key1 Key2
---- ----
A1   B1
A2   B2
A3   B3

Workaround

One workaround is to use success stream instead of information stream, ex Write-Output:

function Test-Stream2
{
	foreach ($i in 1..3)
	{
                # Using success stream instead, not good idea!
		Write-Output "Performing operation $i" -INFA "Continue"

		[PSCustomObject]@{
			Key1 = "A$i"
			Key2 = "B$i"
		}
	}
}

Test-Stream2

Which results in output shown in Expected behavior, but problem is when we need to select property, ex:

Test-Stream2 | Select-Object -Property Key1

This is even worse because now we not only lost those informational messages, but the output doesn't look right either.

Key1
----

A1

A2

A3

This is relatively simple example, since all of the output will still be on same screen, but often enough output can span 20 or more lines or consist of multiple unrelated outputs, and in that case how do you figure out which informational message is referring to which line or output?

You see a bunch on informational message in one place and a bunch of output in another place which looks like there is no connections between them and it kinds of defeats the purpose of non success streams. (at least when it comes to showing them in the console)

Expected behavior would be to print output sequentially instead of separating data and other streams into chunks.

Off course, same behavior happens with other streams: Write-Warning, Write-Error, Write-Debug etc.. but at least error stream will tell us on which line something happened, but that's not the case with other streams.

I'm sure there must be some rationale behind current design, and if so, a switch parameter that would let us have expected behavior would be great, ex:

# Wait until success stream is done processing
Write-Information -Message "Sample Operation" -Wait

Additional related unexpected behavior

Please run both functions in same time as follows:

Test-Stream
Test-Stream2

There is no way this should be expected behavior?

Performing operation 1

Performing operation 2
Performing operation 3
Performing operation 1
Performing operation 2
Performing operation 3
Key1 Key2
---- ----
A1   B1
A2   B2
A3   B3
A1   B1
A2   B2
A3   B3

At least those 2 function outputs (including streams) should have been separated, because now it looks like there is one output while there are 2.
And if you go ahead and modify my second function Test-Stream2 then it's going to be even worse because there will be no output at all:

# Modification of Test-Stream2, Test-Stream stays the same
function Test-Stream2
{
	foreach ($i in 1..3)
	{
		Write-Information -MessageData "Performing operation $i" -INFA "Continue"

		[PSCustomObject]@{
			UNRELATED_KEY1 = "A$i"
			UNRELATED_KEY2 = "B$i"
		}
	}
}

Test-Stream
Test-Stream2
Performing operation 1

Performing operation 2
Performing operation 3
Performing operation 1
Performing operation 2
Performing operation 3
Key1 Key2
---- ----
A1   B1
A2   B2
A3   B3

<output is missing>

As you can see, the info messages are telling us both functions run, but second one didn't print anything, in fact it's not clear from this output which one failed and why.

If those informational messages would have been printed then we could tell at which point the output stopped working.

If after all these samples I'm still wrong, please let me know how to achieve what I'm trying to do.

EDIT:
Btw, I know we should use hash tables for key/value pairs, but this is just an example, and is reproducable with "non key/value pair" objects, create any PSCustomObject you like and the result will be the same.
And Format-Table doesn't work for me, it only separates 2 functions, but streams are still unrelated to output.

I should have titled this issue with PSCustomObject but I guess it's too late now...

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@metablaster metablaster added the Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a label Dec 19, 2020
@mklement0
Copy link
Contributor

Please see #4594

@iSazonov iSazonov added the Resolution-Answered The question is answered. label Dec 19, 2020
@metablaster
Copy link
Author

Thank you, I searched for this problem for some time but couldn't find this issue.
I'll ask my additional question in the link you posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.
Projects
None yet
Development

No branches or pull requests

3 participants