Powershell: Test-Connection shows unwanted output when DNS not resolved on Powershell 7.0.0

Created on 18 Mar 2020  路  12Comments  路  Source: PowerShell/PowerShell

Test-Connection -Quiet -Count 1 Hostname
gives output

Test-Connection: Testing connection to computer 'Hostname' failed: Cannot resolve the target name.

Expected behaviour would be

False

In a code block like
$comps | ForEach-Object -Process { if (Test-Connection $_.Name -Quiet -Count 1) {Write-Output "$($_.Name) on"} else {Write-Output "$($_.Name) off"} }
this gives output
Test-Connection: Testing connection to computer 'Hostname' failed: Cannot resolve the target name. Hostname off

Again, the extra output is unwanted, given the Quiet flag.

Area-Cmdlets-Utility Issue-Bug Resolution-Fixed

Most helpful comment

:tada:This issue was addressed in #12204, which has now been successfully released as v7.1.0-preview.2.:tada:

Handy links:

All 12 comments

馃 This is kinda weird. We shouldn't be surfacing any hostname resolution errors...

This code path handles the hostname resolution:

https://github.com/PowerShell/PowerShell/blob/43e0e1a1c54144ae351e1541dd0965b7792d338b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs#L640-L733

As you can see, we're catching everything in there. Not sure why you'd be getting hostname resolution errors here, unless you're using a different Test-Connection command. There are only two cases you should be getting errors from this cmdlet -- when you provide a $null hostname, or when you provide an IP address that doesn't match the requested switch (i.e., providing an IPv6 address and specifying -IPv4 or vice versa).

Just to be sure, can you grab the output from Get-Command Test-Connection so we can see what it's calling?

Get-Command Test-Connection

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Test-Connection                                    7.0.0.0    Microsoft.PowerShell.Management

Can you post the result from $PSVersionTable as well, please, so we have any necessary OS / other info? Thanks! 馃槉

We catch the hostname resolution error in the method and call WriteError() -- there's nothing that can be done outside the method call itself other than writing false to output. The issue here is that the error still shows in -Quiet mode, so we'd need to handle that in the method.

Quiet parameter can be used in interactive session too. If user don't want to get error SilentlyContinue exists.

-Quiet should do what it says on the tin, in my opinion. 馃し鈥嶁檪

If it's erroring, it's not quiet. A user shouldn't have to ask for quiet twice in one command call. That just leads to frustration, at best. If they want the error, they can simply not use -Quiet.

@vexx32 btw the get host entry call is part of why the command sometimes doesn't respond to stop processing requests.

That's a very good point... Is there an async/cancellable method for that one you know of?

EDIT: Nevermind, found one! https://docs.microsoft.com/en-us/dotnet/api/system.net.dns.gethostentryasync?view=netcore-3.1

I _think_ we can incorporate that one... Not sure exactly how, but seems like it should be doable to add some code to make it respect cancellation requests.

@vexx32 Since there's no cancel, all you can really do is something like:

private CancellationTokenSource _cancel;

// ...

private IPHostEntry GetHostEntryWithCancel(string hostNameOrAddress)
{
    var task = Dns.GetHostEntryAsync(hostNameOrAddress);
    var waitHandles = new[] { ((IAsyncResult)task).AsyncWaitHandle, _cancel.Token.WaitHandle };
    if (WaitHandle.WaitAny(waitHandles) == 1)
    {
        throw new OperationCanceledException();
    }

    return task.GetAwaiter().GetResult();
}

:tada:This issue was addressed in #12204, which has now been successfully released as v7.1.0-preview.2.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HumanEquivalentUnit picture HumanEquivalentUnit  路  3Comments

aragula12 picture aragula12  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

concentrateddon picture concentrateddon  路  3Comments

andschwa picture andschwa  路  3Comments