Skip to content

PSReviewUnusedParameter not finding script-param inside function scope (implicit and explicit) #2017

Closed
@LukasGelke

Description

@LukasGelke

Steps to reproduce

implicit:

param(
  [string]$name
)

function Invoke-Main
{
  "$name"
  "$script:name"
}

Invoke-Main
> .\issue.ps1 "abc"
abc
abc

explicit:

param(
  [string]$name
)

function Invoke-Main($name)
{
  "$name"
  "$script:name"
}

Invoke-Main "123"
> .\issue.ps1 "abc"
123
abc

both scripts work as expected.

Expected behavior

no PSReviewUnusedParameter in either case.
(Perhaps some "ambiguous" or "shadowing" warning/info in the second case, but that's not the point here)

Actual behavior

(both cases)

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      issue.ps1  2     The parameter 'name' has been declared but not used.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.4.4
PSEdition                      Core
GitCommitId                    7.4.4
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

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

Notes

We have several scripts that work similar to this (script-param used in "script-internal" functions) due to try-finally, try.catch, logging, or params like Endpoint-URI, Database-Name/Address,... that would not change during script execution. They are scripts, intended (and used as such) to be ran on their own, not modules. It would be horrible to read an maintain, if we had to explicitly pass all script-params to every relevant function again.
But it would still be ok if every reference had to use the $script: syntax, for strictness/debugging/maintainability/etc.

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