Skip to content

Test-Connection cmdlet displaying unwanted data. #6768

Closed

Description

@NJ-Dude

Steps to reproduce

The Test-Connection cmdlet is displaying unwanted data as part of the result.

Code:

Test-Connection www.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

Activity

  1. added
    Issue-Discussion
    on Apr 28, 2018
  2. added
    Issue-Bug
    and removed
    Issue-Discussion
    on Apr 30, 2018
  3. BrucePay commented on Apr 30, 2018

    @BrucePay
    Collaborator

    This works properly in 5.1 but not in 6 so I've reclassified the issue as a bug.

  4. GeeLaw commented on May 1, 2018

    @GeeLaw

    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 InformationAction common parameter. Example:

    Test-Connection www.microsoft.com -Count 1 -Quiet -InformationAction Continue

    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.

  5. iSazonov commented on May 19, 2018

    @iSazonov
    Collaborator

    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.

  6. mklement0 commented on May 19, 2018

    @mklement0
    Contributor

    Ji Luo (@GeeLaw):

    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 Ignore is 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.

  7. GeeLaw commented on May 20, 2018

    @GeeLaw

    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.,

    Test-Connection www.microsoft.com -Count 1 -Quiet 6> $null

    Check the correctness by the following:

    Write-Host 'Hello, world' 6> $null

    It should write nothing to the host.

  8. mklement0 commented on May 20, 2018

    @mklement0
    Contributor

    Ji Luo (@GeeLaw):

    It seems to me that InformationPreference (InformationAction) won't accept Ignore?

    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:

    Test-Connection www.microsoft.com -Count 1 -Quiet 6> $null
    Test-Connection www.microsoft.com -Count 1 -Quiet -InformationAction Ignore

    In other words: both 6> $null and -InformationAction Ignore are effective workarounds.

    Is InformationActionPreference a new alias for InformationPreference?

    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 / $ErrorActionPreference pair where the preference variable name retains the Action part.


    As an aside regarding the prohibition of Ignore as the value of action preference variables:

  9. GeeLaw commented on May 20, 2018

    @GeeLaw

    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:

    function test-func { [cmdletbinding()]param() process { $InformationPreference } }
    test-func # gives SilentlyContinue
    test-func -InformationAction Continue # gives Continue
    test-func -InformationAction Ignore # gives Ignore

    In 5.1 and 6.0.2, InformationAction does not accept Ignore, as can be demonstrated by the following snippet:

    function test-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.

  10. GeeLaw commented on May 20, 2018

    @GeeLaw

    Another round of playing around shows the following code works:

    Write-Information 'writing' -InformationAction Ignore

    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.,

    Function Test-Inf1
    {
        [CmdletBinding()] Param ( )
        Process { Write-Information 'Hello, world!' }
    }
    Function Test-Inf2
    {
        [CmdletBinding()] Param ( )
        Process { If ($InformationPreference -ne 'Ignore') { Write-Information 'Hello, world!' } }
    }
    Test-Inf1 -InformationAction Ignore # writes an error
    Test-Inf2 -InformationAction Ignore # okay

    It seems that the same applies to authors writing cmdlets with C#.

  11. mklement0 commented on May 20, 2018

    @mklement0
    Contributor

    Ji Luo (@GeeLaw)

    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 -InformationAction does 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.

  12. mklement0 commented on May 20, 2018

    @mklement0
    Contributor

    To summarize, we've been discussing two unrelated problems here:

    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[]] @()) } 
  13. tibmeister commented on Sep 19, 2018

    @tibmeister

    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.

  14. 47 remaining items

  15. vexx32 commented on Jun 21, 2019

    @vexx32
    Collaborator

    Mainly what you think the UX would be like for that, I think. :)

  16. iSazonov commented on Jun 21, 2019

    @iSazonov
    Collaborator

    In interactive session best UX is

    1. minimal typing
      It means:
    • ping is default
    • switching to another mode (traceroute and etc.) with one parameter
    1. 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.
  17. vexx32 commented on Jun 21, 2019

    @vexx32
    Collaborator

    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.

  18. iSazonov commented on Jun 21, 2019

    @iSazonov
    Collaborator

    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.

  19. vexx32 commented on Jun 21, 2019

    @vexx32
    Collaborator

    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.

  20. SteveL-MSFT commented on Oct 3, 2019

    @SteveL-MSFT
    Member

    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:

    PS C:\Users\slee> Test-Connection localhost
    
    Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
    ------        -----------     -----------      -----------                              -----    --------
    SLEE-DESKTOP  localhost       127.0.0.1        ::1                                      32       0
    SLEE-DESKTOP  localhost       127.0.0.1        ::1                                      32       0
    SLEE-DESKTOP  localhost       127.0.0.1        ::1                                      32       0
    SLEE-DESKTOP  localhost       127.0.0.1        ::1                                      32       0
    

    However, with your change, all the ping replies are members of a single object:

    PS> Test-Connection localhost
    
    Source       Destination Replies
    ------       ----------- -------
    slee-desktop localhost   {System.Net.NetworkInformation.PingReply, System.Net.NetworkInformation.PingReply, System.Net.NetworkInformation.PingReply, System.Net.N…
    

    Are we not able to get back the Windows PowerShell behavior?

  21. vexx32 commented on Oct 3, 2019

    @vexx32
    Collaborator

    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. 😊

  22. vexx32 commented on Oct 3, 2019

    @vexx32
    Collaborator

    Steve Lee (@SteveL-MSFT) see #10697 for the next chapter in this adventure! 😊

  23. chvol commented on Mar 18, 2020

    @chvol

    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

  24. vexx32 commented on Mar 18, 2020

    @vexx32
    Collaborator

    chvol could you please open a new issue for that with as much detail as possible?

    Thanks! 🙂

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

    Committee-ReviewedIssue-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions