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.
馃 This is kinda weird. We shouldn't be surfacing any hostname resolution errors...
This code path handles the hostname resolution:
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! 馃槉
Oh, I see it.
We should definitely be just returning false here.
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:
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: