Closed etamasdan-nearform closed 2 months ago
Hey @etamasdan-nearform 👋,
The intent of the PossibleIncorrectUsageOfRedirectionOperator
rule is to catch possible incorrect usage.
A Redirect operator in the clause of an if-statement is pretty niche.
I appreciate you're using it intentionally here, but in most cases the redirect operator is used in error - by those more familiar with other coding languages or switching coding languages frequently. I include myself as someone that has used it in error many times!
I'd, personally, resolve this in my script by first capturing the command output, then performing the check.
$commandOutput = command 2> $null
if (!$commandOutput) {
...
}
You can also tell PSScriptAnalyzer, in a few ways, to ignore this rule if it isn't to your personal preference.
Supress the rule explicitly in the file or function in which the code appears:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectUsageOfRedirectionOperator")]
Param()
Perhaps more sustainably, you could create a settings file that has your own preferences in it. Something like the below perhaps?
# PSScriptAnalyzerSettings.psd1
@{
ExcludeRules=@(
'PSPossibleIncorrectUsageOfRedirectionOperator'
)
}
Then you could tell the Powershell extension in VSCode to use that settings file.
"powershell.scriptAnalysis.settingsPath": ""
Hope that helps 😀!
Summary of the new feature
As a developer I have valid Powershell code in my if-statement and I expect zero warnings but my IDE is displaying a warning:
The above code has
2> $null
highlighted with the warning:PossibleIncorrectUsageOfRedirectionOperator
.Proposed technical implementation details (optional)
Revert or redo this offending PR: https://github.com/PowerShell/PSScriptAnalyzer/pull/881
What is the latest version of PSScriptAnalyzer at the point of writing
How do I obtain this? The one that comes with VS Code > ms-vscode's PowerShell extension.