question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReviewUnusedParameter does not capture parameter usage within a scriptblock

See original GitHub issue

Steps to reproduce

Run Invoke-ScriptAnalyzer against the following with the new 1.19.0 release.

function foo {
    Param(
        $MyParameter
    )

    Get-Item| ForEach-Object { Get-ChildItem $MyParameter }
}

Expected behavior

No rule violations.

Actual behavior

The new ReviewUnusedParameter rule doesn’t notice the usage. I suspect this is similar to the limitation of the AvoidUsingCmdletAliases rule though. Not sure if we should relax the ReviewUnusedParameter rule in this case to search nested scriptblocks inside a function scope. cc @mattmcnabb @rjmholt @JamesWTruher

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      test.ps1   4     The parameter 'MyParameter' has been declared but not used.

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.1.0-preview.2
PSEdition                      Core
GitCommitId                    7.1.0-preview.2
OS                             Microsoft Windows 10.0.18363
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.19.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:31
  • Comments:27 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
michielvoocommented, Aug 17, 2022

I have tried suppressing the warning as you suggested:

[Parameter(Mandatory=$false, HelpMessage="The SystemAttributeValue.ObjectId property (Required)")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '$ObjectId', Justification = 'false positive')]

But I am still getting the PSScriptAnalyzer warning. What am I doing wrong?

To suppress PSReviewUnusedParameter for a specific parameter you should set SuppressMessageAttribute’s checkId to the name of the parameter without the $ prefix.

2reactions
JPRuskincommented, May 6, 2020

In this case, the simple solution is to also look in the child scriptblock. The better solution is to search the child scriptblock when the command is ForEach-Object or Where-Object

To add to the list, we’re also seeing this fail with Invoke-Command -ScriptBlock {} (though this using $using:varName), and with usage via @PSBoundParameters, which is frustrating. I’m unsure what to suggest, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReviewUnusedParameter - PowerShell
This rule identifies parameters declared in a script, scriptblock, or function scope that have not been used in that scope.
Read more >
Examples/cTentacleAgent_ListeningTentacle.psm1 4.0.957
SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="ReviewUnusedParameter does not capture parameter usage within a scriptblock.
Read more >
Why does an argument passed into a script block fail to ...
The reason is because the entire xss variable is being printed as an argument and is not named inside the job. It's given...
Read more >
What is wrong with this Command? Warning" 'Cannot ...
Warning" 'Cannot evaluate parameter 'NewName' because argument is specified as a script block and there is no input'.
Read more >
Chris Bergmeister
Another new rules is soon coming to the next version of #PSScriptAnalyzer: ReviewUnusedParameter warns if a parameter is declared in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found