PowerShell / PSScriptAnalyzer

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

PSReviewUnusedParameter marks script parameter as unused #1923

Closed kristianshevando closed 11 months ago

kristianshevando commented 1 year ago

Steps to reproduce

[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [System.String]$UserName,

    [Parameter(Mandatory = $true)]
    [System.Security.SecureString]$Password,

    [Parameter(Mandatory = $true)]
    [System.String]$TenantId,

    [Parameter(Mandatory = $false)]
    [System.Int32]$DaysToKeep = 30,

    [Parameter(Mandatory = $false)]
    [System.String]$UsersLimit = 30,

    [Parameter(Mandatory = $false)]
    [bool]$Force = $false
)

#did not put the whole script because it's kinda big

$inactiveUsers = $users.value | Where-Object {
    ($_.accessLevel.licenseDisplayName -eq "Basic" -or $_.accessLevel.licenseDisplayName -eq "Basic + Test Plans" -or $_.accessLevel.licenseDisplayName -eq "Stakeholder") -and
    ($_.user.principalName -notin $usersWhitelist) -and
    ($_.user.mailAddress -ne $organizationOwnerEmail) -and
    ([DateTime]::Parse($_.lastAccessedDate) -lt (Get-Date).AddDays(-$DaysToKeep))
    }

Expected behavior

No warnings

Actual behavior

Invoke-ScriptAnalyzer -Path .\RemoveInactiveUsers.ps1 -Recurse

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      RemoveInac 55    The parameter 'DaysToKeep' has been declared but not used.
                                                 tiveUsers.
                                                 ps1

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.3.4
PSEdition                      Core
GitCommitId                    7.3.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.21.0
Hrxn commented 11 months ago

Yeah, I guess I have the same problem.

PSScriptAnalyzer version 1.21.0, PowerShell version 7.3.6.

My allegedly unused parameter is a [switch] parameter (although [bool] has the same issue, just FYI). I have more than one, actually, and all of them work as expected, the "offending" switch is actually used inside a function defined in the script file (The switches at the "top" level of the script file works as expected).. So maybe PSScriptAnalyzer tries to be clever, and thinks the code inside the function is unreachable? Although that is not the case.. Well, it depends on the other switches in my case, i.e. there is a possible code path that indeed does not reach the check for the "offending" switch parameter, but otherwise this gets used and works as one would expect..

SydneyhSmith commented 11 months ago

Thanks @kristianshevando for opening this and providing repro steps... this is a known issue caused by how PSSA handles scoping for the Where-Object script block, unfortunately this is a very difficult bug to fix with the current architecture of PSSA-- going to mark this as a duplicate

Hrxn commented 11 months ago

@SydneyhSmith Now both #1923 and #1924 are marked with Resolution - Duplicate, so which one should be used for tracking this in the future?

SydneyhSmith commented 11 months ago

@Hrxn apologies should have tagged another issue, marking as a duplicate of #1472

ghost commented 11 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.