Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

PSReviewUnusedParameter generates a warning parameter if we use $MyInvocation.MyCommand #1575

Copy link
Copy link
@aikiox

Description

@aikiox
Issue body actions

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.

function Format-ConditionQuery ($parameters) {
    $Condition = $null
    $ExcludeList = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable'
    foreach ($Params in ($parameters | Where-Object { $_ -notin $ExcludeList })) {
        $GetContentOnParam = $null
        $GetContentOnParam = Get-Variable $Params
        if ($GetContentOnParam.Value) {
            $Value = $GetContentOnParam.Value.tostring().replace("'", "''")
            $operator = ' = '
            $conditionOperator = " where "
            if ($condition) {
                $conditionOperator = " and "
            }
            $Condition += (" {0} [{1}] {2} '{3}' " -f $conditionOperator, $Params, $operator, $Value )
        }
    }
    return $condition
}
Function foo  {
    Param
    (
        [Parameter(Position = 0)]
        $id,
        [Parameter(Position = 1)]
        $name
    )

    Begin {

    }
    Process {
        $Condition = ''
        $Condition = Format-ConditionQuery -parameters ($MyInvocation.MyCommand).parameters.Keys
        if ($Condition) {
            $Condition
        }
        else {
            Write-Error -Message "No condition generate"
        }
    }
    End {

    }
}
foo -name "ok"

Expected behavior

No rule violations.

Actual behavior

The new PSReviewUnusedParameter rule doesn't notice the usage.

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      TryAnalyse 23    The parameter 'id' has been declared but not used.
                                                 .ps1
PSReviewUnusedParameter             Warning      TryAnalyse 25    The parameter 'name' has been declared but not used.        
                                                 .ps1

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.145
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.145
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.19.1
1.19.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.