PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.87k stars 379 forks source link

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

Closed LukasGelke closed 3 months ago

LukasGelke commented 3 months ago

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.

SydneyhSmith commented 3 months ago

Thanks for the issue this looks like a duplicate of #1472

microsoft-github-policy-service[bot] commented 3 months ago

This issue has been marked as duplicate and has not had any activity for 1 day. It will be closed for housekeeping purposes.