You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Test-Connection cmdlet is displaying unwanted data as part of the result.
Code:
Test-Connectionwww.microsoft.com-Count 1-Quiet
Expected behavior
It should display just the word: True
Actual behavior
Pinging www.microsoft.com [23.204.153.19] with 32 bytes of data:
Reply from 23.204.153.19: bytes=32 time=17ms TTL=58
Ping complete.
True
Environment data
>$PSVersionTable
Name Value
---------
PSVersion 6.1.0-preview.2
PSEdition Core
GitCommitId v6.1.0-preview.2
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
Reacted by Jonas Thelemann, thom-s, Steve Lee, Chris Dent, Chris Gardner and Antanas Domarkas
From scripting perspective, this wouldn't be an issue, as the textual information is never written to the pipeline, and no, the textual output is not part of result data, which are defined as things sent to the pipeline. Also, Quiet switch is defined to return simpler results (int or bool, instead of record objects). I have to admit that one might not expect InformationRecord with Quiet. However, knowing the reason, I say we'd better keep InformationAction and Quiet decoupled.
In PowerShell 5.1, Test-Connection does not seem to call WriteInformation at all. By the way, the default value for $InformationPreference in PowerShell 5.1 and PowerShell Core 6.0.2 is SilentlyContinue. The author of the issue might have a different effective value when he reproduced the issue. (Perhaps PS 6.1 Core changed the default value for $InformationPreference? I'm not sure.)
If PS 6.1 Core had $InformationPreference defaulted to SilentlyContinue, the textual information wouldn't be there unless the user explicitly asks for it.
I need more feedback.
The problem is that in the script mode and in interactive mode it should work in different ways. In interactive mode the user will probably prefer to see the progress (bar) as it happens with the ping.exe command. This applies to other parameters too.
Good find, but it is -InformationAction Ignore that is needed in this case to work around the bug.
$InformationActionPreference still defaults to SilentlyContinue, and that shouldn't change.
The bug is that the WriteInformation() calls you link to mistakenly use the PSHOST tag, which effectively bypasses the $InformationActionPreference value and also -InformationAction SilentlyContinue (but, as stated, -InformationAction Ignoreis effective in suppressing the output).
What said WriteInformation() calls do is effectively what Write-Host does in order to unconditionally display its output (by design).
Ilya (@iSazonov): I haven't really looked at the behavior of other cmdlets with progress bars, but with -Quiet I would not expect a progress bar, even when calling interactively.
Reacted by Lyon Till, Jonas Thelemann, Konstantin and thom-s
Michael Klement (@mklement0) Thanks for the reply and the correction on PSHostTag. It seems to me that InformationPreference (InformationAction) won't accept Ignore? This is true in 5.1 and 6.0.2. Perhaps in 6.1 it's changed. (Is InformationActionPreference a new alias for InformationPreference?)
Also, my first comment was mistaken, for that it sets InformationAction to Continue. The correct workaround is to discard stream 6 (information stream) by redirecting it to $null, i.e.,
Michael Klement (@mklement0) I was asking about the alias because you mentioned it as InformationActionPreference.
To my best knowledge, -XxxAction (and -Verbose and -Debug) simply sets the corresponding preference variable inside the invoked cmdlet. As documented in about_ help topics, specifically we have the following:
The value of the -InformationAction parameter, if used, overrides the current value of the $InformationPreference variable.
Within the command or script in which it is used, the InformationAction common parameter overrides the value of the $InformationPreference preference variable, which by default is set to SilentlyContinue.
I interpret this as setting a local preference variable, as can be validated by the following demonstration:
In 5.1 and 6.0.2, InformationAction does not accept Ignore, as can be demonstrated by the following snippet:
functiontest-func { [cmdletbinding()]param() process { write-information'writing' } }
test-func-InformationAction Ignore
produces
Write-Information : The value Ignore is not supported for an ActionPreference variable. The provided value should be used only as a value for a preference
parameter, and has been replaced by the default value. For more information, see the Help topic, "about_Preference_Variables."
At line:1 char:57
+ ... { [cmdletbinding()]param() process { Write-Information 'writing' } }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Information], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.WriteInformationCommand
I wonder if that changed in 6.1.
There's another interesting thing: $VerbosePreference and $DebugPreference actually accepts a wider range than that can be set by the corresponding common parameter. E.g., it is possible to set $VerbosePreference to Inquire by explicit assignment, but not possible by using -Verbose switch because, after all, it is a switch and maps $True/$False to Continue/SilentlyContinue.
As far as I know, except for those two switches, the preference-related common parameters map exactly to the corresponding preference variables in the local scope.
But that would be cumbersome to developers, as we have to guard Write-Information with the check $InformationAction -eq 'Ignore', and avoid calling it in the first place, e.g.,
FunctionTest-Inf1
{
[CmdletBinding()] Param ( )
Process { Write-Information'Hello, world!' }
}
FunctionTest-Inf2
{
[CmdletBinding()] Param ( )
Process { If ($InformationPreference-ne'Ignore') { Write-Information'Hello, world!' } }
}
Test-Inf1-InformationAction Ignore # writes an errorTest-Inf2-InformationAction Ignore # okay
It seems that the same applies to authors writing cmdlets with C#.
I was asking about the alias because you mentioned it as InformationActionPreference.
Oops! Sorry - hadn't noticed my own typo.
simply sets the corresponding preference variable inside the invoked cmdlet.
Yes, and the rest of what you demonstrate is the subject of the aforementioned #1759.
In short: The design flaw is that disallowing Ignore as a preference-variable value clashes with using automatically set local preference-variable instances in order to propagate common-parameter values.
More specifically:
In 5.1 and 6.0.2, InformationAction does not accept Ignore, as can be demonstrated by the following snippet:
Since it matters with respect to resolving the problem, let me point out that -InformationActiondoes accept Ignore, and it's only said design flaw that causes the problem if and when the preference is applied in the context of advanced functions, which in your case is the Write-Information call inside the function .
This problem persists as of PowerShell Core v6.1.0-preview.2.
Compiled cmdlets, by contrast, do not have this problem, which is why Write-Host foo -InformationAction Ignore works as intended, for instance, as you've since discovered.
This issue's original topic, the misbehaving Test-Connection cmdlet, which mistakenly uses the PSHOST tag in its calls to .WriteInformation.
The solution to the latter is to simply omit the tag, as the following example demonstrates:
# With $InformationAction at its default, 'SilentlyContinue', this invocation is silent.# If you set it to 'Continue', 'foo' prints.& { [CmdletBinding()]param() $PSCmdlet.WriteInformation('foo', [string[]] @()) }
So I came across this because I am experiencing the same issue where -Quiet is not being respected. I looked at the code and it's a little difficult to follow and very over-complicated for what it’s doing, but I didn't see anywhere that is actually looking for this as a matter of suppressing output, nor am I seeing where it's switching from outputting a string to a boolean, which is what should be outputted when the -Quiet parameter is passed.
I did submit a PR last year (#2537) to add Test-Connection to the code and that was rejected at the time because “@PowerShell/powershell-committee would not accept this PR as it would introduce compatibility problems in the future”, so I was surprised to see the inclusion of this cmdlet now but not with the code of the original PR but rather brand new code that doesn’t provide all the expected functionality.
The biggest issue I come across is in my scripts I perform checks like this, “if(Test-Connection 8.8.8.8 -Quiet)” to branch into my logic, but with the -Quiet parameter not being respected, the branch always returns True because there’s not a False or Null. So this makes the command still completely unusable to me, and also since it’s been included in new releases of PowerShell, that makes upgrading very touchy for me. Please, fix this as it looks like it’s been several months since the issue was first reported. Either that, or re-instate the original PR, doesn’t matter as long as the functionality is returned.
I did submit a PR last year to add Test-Connection to the code and that was rejected at the time because they didn't want to add legacy cmdlets, so I was surprised to see the inclusion of this cmdlet now but not with the code of the original PR that was complete and functioned exactly as the "legacy" version did.
Reacted by Jonas Thelemann, Aleksandar Nikolić and NJ-Dude
switching to another mode (traceroute and etc.) with one parameter
user friendly output
It means:
emulate ping.exe (tracert.exe and others) output on console host like I tried in the demo code - with headers, footers and informative lines well formatted. No need to think about output types - they is not used, only displayed.
add parameters to switch to script mode - that is to suppress output of user-friendly text on console host and emit strong typed objects. No need to format this output. We discussed Quiet which returns True/False but we need a parameter(s) to emit other raw strong typed objects (like -RawOutput). It is acceptable UX to use additional parameters in scripts.
Thanks, I think I understand what you're getting at a bit better now.
I don't really see the need for a dual mode like this, though? No other cmdlets in PowerShell have this split between interactive and "script mode" parameters.
If you wanted the exact output of ping / tracert, why wouldn't you just use those utilities directly?
PowerShell has never made a significant effort to completely mimic an existing command; I think Get-ChildItem is probably the closest, but it's almost the only one to do so.
If we wanted to thoroughly emulate ping / tracert display like you say, I would suggest we instead have that as a separate cmdlet or function, e.g., Show-Connection, rather than clutter Show-Command with extra parameters that have no existing precedent or need within PowerShell.
I don't really see the need for a dual mode like this, though? No other cmdlets in PowerShell have this split between interactive and "script mode" parameters.
We have gaps in out formatting system. For example, we have an issue with request to have header/footer dir. There are other scenarios where it would be convenient.
If you wanted the exact output of ping / tracert, why wouldn't you just use those utilities directly?
I do :-). These native utilities are very powerful because they are low-level but they are frozen. We can do smarter things than they do - this is the essence of the appearance of this cmdlet (and not just to make a ping) - if you take a deep look at how the addresses and names in the cmdlet are now formed and how they are output, you will see that there are much more useful than in the native ping. It looks like tricks but it is really very useful.
Of course we have gaps in the formatting system. But I don't think the answer is to implement a custom solution every single time. That just creates more problems and makes it even more difficult to improve or rewrite the formatting system to be more effective across the board.
Yeah, you've done some great work with it! I very much appreciate that. The current mode of output is not useful for anything but interactive use. We could simply remove the output step and call it Show-Connection and then utilise what you've written in a more PowerShell-focused solution for Test-Connection itself as I outline in the RFC.
Rain Sallow (/u/ta11ow) (@vexx32) I just tried your PR and it improves it quite a bit, but I noticed that in Windows PowerShell, it emits PingReply individually to the pipeline so you get something that looks like:
Steve Lee (@SteveL-MSFT) That object output has not changed from the initial implementation in PS Core; the success output has always had the single object. 🙂
As mentioned in the closing comments of the PR, that is only a partway implementation of the RFC to help simplify reviews. I will be submitting a follow-up PR shortly. Just have to rebase that branch to remove the now-duplicate commits and submit the rest of it to get much closer to true parity with Windows PowerShell's implementation. It will still differ somewhat (as can be seen from the RFC we reviewed a handful of weeks back) but will be hopefully much more versatile. 😊
In release 7.0.0 Test-Connection -Quiet still gives
Test-Connection: Testing connection to computer 'scrubbed' failed: Cannot resolve the target name.
instead of
False
Activity
BrucePay commentedon Apr 30, 2018
This works properly in 5.1 but not in 6 so I've reclassified the issue as a bug.
GeeLaw commentedon May 1, 2018
The reason is that current code calls
WriteInformation(blindly?).See line 751 of TestConnectionCommand.cs, also line 775 and line 783.
Temporary workaround to stop the information from being displayed to the host is to use
InformationActioncommon parameter. Example:From scripting perspective, this wouldn't be an issue, as the textual information is never written to the pipeline, and no, the textual output is not part of result data, which are defined as things sent to the pipeline. Also,
Quietswitch is defined to return simpler results (intorbool, instead of record objects). I have to admit that one might not expectInformationRecordwithQuiet. However, knowing the reason, I say we'd better keepInformationActionandQuietdecoupled.In PowerShell 5.1,
Test-Connectiondoes not seem to callWriteInformationat all. By the way, the default value for$InformationPreferencein PowerShell 5.1 and PowerShell Core 6.0.2 isSilentlyContinue. The author of the issue might have a different effective value when he reproduced the issue. (Perhaps PS 6.1 Core changed the default value for$InformationPreference? I'm not sure.)If PS 6.1 Core had
$InformationPreferencedefaulted toSilentlyContinue, the textual information wouldn't be there unless the user explicitly asks for it.iSazonov commentedon May 19, 2018
I need more feedback.
The problem is that in the script mode and in interactive mode it should work in different ways. In interactive mode the user will probably prefer to see the progress (bar) as it happens with the ping.exe command. This applies to other parameters too.
Michael Klement (@mklement0) If you have time then your help would be useful.
mklement0 commentedon May 19, 2018
Ji Luo (@GeeLaw):
Good find, but it is
-InformationAction Ignorethat is needed in this case to work around the bug.$InformationActionPreferencestill defaults toSilentlyContinue, and that shouldn't change.The bug is that the
WriteInformation()calls you link to mistakenly use thePSHOSTtag, which effectively bypasses the$InformationActionPreferencevalue and also-InformationAction SilentlyContinue(but, as stated,-InformationAction Ignoreis effective in suppressing the output).What said
WriteInformation()calls do is effectively whatWrite-Hostdoes in order to unconditionally display its output (by design).Ilya (@iSazonov): I haven't really looked at the behavior of other cmdlets with progress bars, but with
-QuietI would not expect a progress bar, even when calling interactively.GeeLaw commentedon May 20, 2018
Michael Klement (@mklement0) Thanks for the reply and the correction on
PSHostTag. It seems to me thatInformationPreference(InformationAction) won't acceptIgnore? This is true in 5.1 and 6.0.2. Perhaps in 6.1 it's changed. (IsInformationActionPreferencea new alias forInformationPreference?)Also, my first comment was mistaken, for that it sets
InformationActiontoContinue. The correct workaround is to discard stream 6 (information stream) by redirecting it to$null, i.e.,Check the correctness by the following:
It should write nothing to the host.
mklement0 commentedon May 20, 2018
Ji Luo (@GeeLaw):
Yes, the preference variable doesn't accept
Ignore, but the common parameter does.That is, you can suppress the information stream (number
6) for a given invocation, but not categorically for the whole scope - by design.Therefore, the following two statements are equivalent:
In other words: both
6> $nulland-InformationAction Ignoreare effective workarounds.No, the name has always been
$InformationPreference, following the pattern of$VerbosePreference,$WarningPreference, and$DebugPreference.From what I can tell it's only the
-ErrorAction/$ErrorActionPreferencepair where the preference variable name retains theActionpart.As an aside regarding the prohibition of
Ignoreas the value of action preference variables:There's a fundamental design problem with this restriction, because automatically defined local preference variables are used to propagate common-parameter values inside advanced functions - see -ErrorAction Ignore doesn't work for calling advanced functions #1759
The restriction is not enforced at assignment time, which means you won't see the problem until the preference is (possibly implicitly) applied the next time - see Preventing setting 'Ignore' as the $ErrorActionPreference value is poorly enforced on the command line, and not at all in scripts #4348
$ErrorActionPreference = 'bogus'to& { $ErrorActionPreference = 'bogus' }- seeExplicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable #3483.
GeeLaw commentedon May 20, 2018
Michael Klement (@mklement0) I was asking about the alias because you mentioned it as
InformationActionPreference.To my best knowledge,
-XxxAction(and-Verboseand-Debug) simply sets the corresponding preference variable inside the invoked cmdlet. As documented inabout_help topics, specifically we have the following:I interpret this as setting a local preference variable, as can be validated by the following demonstration:
In 5.1 and 6.0.2,
InformationActiondoes not acceptIgnore, as can be demonstrated by the following snippet:produces
I wonder if that changed in 6.1.
There's another interesting thing:
$VerbosePreferenceand$DebugPreferenceactually accepts a wider range than that can be set by the corresponding common parameter. E.g., it is possible to set$VerbosePreferencetoInquireby explicit assignment, but not possible by using-Verboseswitch because, after all, it is a switch and maps$True/$FalsetoContinue/SilentlyContinue.As far as I know, except for those two switches, the preference-related common parameters map exactly to the corresponding preference variables in the local scope.
GeeLaw commentedon May 20, 2018
Another round of playing around shows the following code works:
But that would be cumbersome to developers, as we have to guard
Write-Informationwith the check$InformationAction -eq 'Ignore', and avoid calling it in the first place, e.g.,It seems that the same applies to authors writing cmdlets with C#.
mklement0 commentedon May 20, 2018
Ji Luo (@GeeLaw)
Oops! Sorry - hadn't noticed my own typo.
Yes, and the rest of what you demonstrate is the subject of the aforementioned #1759.
In short: The design flaw is that disallowing
Ignoreas a preference-variable value clashes with using automatically set local preference-variable instances in order to propagate common-parameter values.More specifically:
Since it matters with respect to resolving the problem, let me point out that
-InformationActiondoes acceptIgnore, and it's only said design flaw that causes the problem if and when the preference is applied in the context of advanced functions, which in your case is theWrite-Informationcall inside the function .This problem persists as of PowerShell Core v6.1.0-preview.2.
Compiled cmdlets, by contrast, do not have this problem, which is why
Write-Host foo -InformationAction Ignoreworks as intended, for instance, as you've since discovered.mklement0 commentedon May 20, 2018
To summarize, we've been discussing two unrelated problems here:
The design flaw with respect to
Ignoreas an action-preference variable value, which is already being tracked in -ErrorAction Ignore doesn't work for calling advanced functions #1759.This issue's original topic, the misbehaving
Test-Connectioncmdlet, which mistakenly uses thePSHOSTtag in its calls to.WriteInformation.The solution to the latter is to simply omit the tag, as the following example demonstrates:
tibmeister commentedon Sep 19, 2018
So I came across this because I am experiencing the same issue where -Quiet is not being respected. I looked at the code and it's a little difficult to follow and very over-complicated for what it’s doing, but I didn't see anywhere that is actually looking for this as a matter of suppressing output, nor am I seeing where it's switching from outputting a string to a boolean, which is what should be outputted when the -Quiet parameter is passed.
I did submit a PR last year (#2537) to add Test-Connection to the code and that was rejected at the time because “@PowerShell/powershell-committee would not accept this PR as it would introduce compatibility problems in the future”, so I was surprised to see the inclusion of this cmdlet now but not with the code of the original PR but rather brand new code that doesn’t provide all the expected functionality.
The biggest issue I come across is in my scripts I perform checks like this, “if(Test-Connection 8.8.8.8 -Quiet)” to branch into my logic, but with the -Quiet parameter not being respected, the branch always returns True because there’s not a False or Null. So this makes the command still completely unusable to me, and also since it’s been included in new releases of PowerShell, that makes upgrading very touchy for me. Please, fix this as it looks like it’s been several months since the issue was first reported. Either that, or re-instate the original PR, doesn’t matter as long as the functionality is returned.
I did submit a PR last year to add Test-Connection to the code and that was rejected at the time because they didn't want to add legacy cmdlets, so I was surprised to see the inclusion of this cmdlet now but not with the code of the original PR that was complete and functioned exactly as the "legacy" version did.
47 remaining items
vexx32 commentedon Jun 21, 2019
Mainly what you think the UX would be like for that, I think. :)
iSazonov commentedon Jun 21, 2019
In interactive session best UX is
It means:
It means:
vexx32 commentedon Jun 21, 2019
Thanks, I think I understand what you're getting at a bit better now.
I don't really see the need for a dual mode like this, though? No other cmdlets in PowerShell have this split between interactive and "script mode" parameters.
If you wanted the exact output of ping / tracert, why wouldn't you just use those utilities directly?
PowerShell has never made a significant effort to completely mimic an existing command; I think Get-ChildItem is probably the closest, but it's almost the only one to do so.
If we wanted to thoroughly emulate ping / tracert display like you say, I would suggest we instead have that as a separate cmdlet or function, e.g.,
Show-Connection, rather than clutter Show-Command with extra parameters that have no existing precedent or need within PowerShell.iSazonov commentedon Jun 21, 2019
We have gaps in out formatting system. For example, we have an issue with request to have header/footer dir. There are other scenarios where it would be convenient.
I do :-). These native utilities are very powerful because they are low-level but they are frozen. We can do smarter things than they do - this is the essence of the appearance of this cmdlet (and not just to make a ping) - if you take a deep look at how the addresses and names in the cmdlet are now formed and how they are output, you will see that there are much more useful than in the native ping. It looks like tricks but it is really very useful.
vexx32 commentedon Jun 21, 2019
Of course we have gaps in the formatting system. But I don't think the answer is to implement a custom solution every single time. That just creates more problems and makes it even more difficult to improve or rewrite the formatting system to be more effective across the board.
Yeah, you've done some great work with it! I very much appreciate that. The current mode of output is not useful for anything but interactive use. We could simply remove the output step and call it
Show-Connectionand then utilise what you've written in a more PowerShell-focused solution forTest-Connectionitself as I outline in the RFC.SteveL-MSFT commentedon Oct 3, 2019
Rain Sallow (/u/ta11ow) (@vexx32) I just tried your PR and it improves it quite a bit, but I noticed that in Windows PowerShell, it emits PingReply individually to the pipeline so you get something that looks like:
However, with your change, all the ping replies are members of a single object:
Are we not able to get back the Windows PowerShell behavior?
vexx32 commentedon Oct 3, 2019
Steve Lee (@SteveL-MSFT) That object output has not changed from the initial implementation in PS Core; the success output has always had the single object. 🙂
As mentioned in the closing comments of the PR, that is only a partway implementation of the RFC to help simplify reviews. I will be submitting a follow-up PR shortly. Just have to rebase that branch to remove the now-duplicate commits and submit the rest of it to get much closer to true parity with Windows PowerShell's implementation. It will still differ somewhat (as can be seen from the RFC we reviewed a handful of weeks back) but will be hopefully much more versatile. 😊
vexx32 commentedon Oct 3, 2019
Steve Lee (@SteveL-MSFT) see #10697 for the next chapter in this adventure! 😊
chvol commentedon Mar 18, 2020
In release 7.0.0 Test-Connection -Quiet still gives
Test-Connection: Testing connection to computer 'scrubbed' failed: Cannot resolve the target name.
instead of
False
vexx32 commentedon Mar 18, 2020
chvol could you please open a new issue for that with as much detail as possible?
Thanks! 🙂