-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PSReviewUnusedParameter generates a warning parameter if we use $MyInvocation.MyCommand #1575
Comments
Thanks @aikiox looks like you are using a paren expression with .parameters rather than .boundparameters, it you are actually not using the values of the parameters, just the name of the parameters...the rule is written for boundparameters (see #1520), additionally the use of parens complicates things in an unexpected way for PSSA. With the .parameters you will be passing along all of the parameters, with .boundparameters you will pass just those that you are using (so you will not need the exclude list). Let us know if we maybe missed something in your script that explains this use case. Thanks! |
To be clear, I think there's a case to be made that not identifying With that said, PSScriptAnalyzer is a static analyser, and just cannot pick up all possible runtime occurrences. In fact there are plenty of cases where it's impossible to detect ahead of time whether behaviour is correct or not. In those situations, it's up to the user to say "I know what I'm doing" and suppress the relevant diagnostic. The important parts here I think that make this something the rule can't be expected to check are:
In the case of this script, I think it's fair that diagnostics are issued; generally that's a warning that your script is hard to reason about. I think an appropriate refactor would use |
Hello @SydneyhSmith and @rjmholt,, Thanks for your help and sorry for the inconvenience. function Format-ConditionQuery ($parameters) {
$Condition = $null
foreach ($Params in $parameters.GetEnumerator()) {
if ($Params.Value) {
$Value = $Params.Value.replace("'", "''")
$operator = ' = '
$conditionOperator = " where "
if ($condition) {
$conditionOperator = " and "
}
$Condition += (" {0} [{1}] {2} '{3}' " -f $conditionOperator, $Params.key, $operator, $Value )
}
}
return $condition
}
Function foo {
Param
(
[Parameter(Position = 0)]
$id,
[Parameter(Position = 1)]
$name
)
Begin {
}
Process {
$Condition = ''
$Condition = Format-ConditionQuery -parameters $PSBoundParameters
if ($Condition) {
$Condition
}
else {
Write-Error -Message "No condition generate"
}
}
End {
}
}
foo -name "ok" -id 'aa' Thanks ! |
Steps to reproduce
In a function, I am using
$MyInvocation.MyCommand
to retrieve the parameters in order to generate an SQL query.Run
Invoke-ScriptAnalyzer
against the following with the new 1.19.1 release.Expected behavior
No rule violations.
Actual behavior
The new PSReviewUnusedParameter rule doesn't notice the usage.
Environment data
The text was updated successfully, but these errors were encountered: