PSCommandNotFoundSuggestion is suggesting commands when it should not
See original GitHub issueSteps to reproduce
If you use Get-Command -ErrorAction Ignore
to test for the existence of a command, you get “Suggestion” output which is confusing to the end user and seems to indicate an error, when in fact there is no problem.
For example, in one script I do this:
$Version = if (Get-Command gitversion -ErrorAction SilentlyContinue) {
gitversion -showvariable semver
} else {
"1.0.0-rc"
}
Expected behavior
When Get-Command
is called and the error is explicitly suppressed, there should be no suggestions printed to the console.
Frankly, it might be better if Get-Command
doesn’t print suggestions at all, or perhaps only when it’s being called interactively from the prompt (and the error isn’t explicitly suppressed).
Actual behavior
I get a completely off-the-wall suggestion from the PSDeploy module:
Suggestion [4,General]: The most similar commands are: WithOptions
Frankly, that command is not at all similar, in my human brain, and it really should not be mentioned at all. I can’t understand how that’s past the similarity threshold.
Environment data
Name Value
---- -----
PSVersion 6.2.1
PSEdition Core
GitCommitId 6.2.1
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:12 (8 by maintainers)
Yeah we need to redo how suggestions are implemented as a whole, really. Currently suggestions are entirely dependent on and coded within PowerShell’s host implementation. Which means they don’t carry over to anyone using a non-default host.
I have a PR #9115 WIP that addresses this, pulls it all out and puts suggestions into a property of the ErrorRecord class with a new data type, which can then be displayed by formatters instead and is accessible to module authors. Got stuck on a few bits and pieces and CI errors that I couldn’t make sense of. I’ll revisit once I’m done working on Test-Connection and see if I can get it properly up to scratch. 🙂
Or, if one of you folks wants to take a stab at it, you’re more than welcome to check it out, add comments, and/or just grab what you need (if anything) and take over as I may or may not have enough spare time to get it done by PS7 (and I’d really like this to be squared away by PS7, but then there are a lot of things I’d like to get done for PS7 hah!)
Yes, I agree that this definitely needs tweaking. But I’ve always also found it annoying that
Get-Command
returns an error if nothing was found (yes, I had to write similar code to yours before as well…). It would be a breaking change but in an ideal world the command should just return nothing and the caller can then write conditional logic. What annoys me about Get-Command as well is that it loads the module into memory by doing a full Import-Module (which makes this check very expensive). For a binary module this means I am stuck with that loaded version since PS cannot unload it (maybe that is possible with recent additions in .Net Core?).In order to avoid breaking changes, what about a more lightweight and user friendly Test-Command cmdlet?