Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSScriptAnalyzer marks unused param but it is used in the script #1924

Closed
ktran1005 opened this issue Jun 28, 2023 · 7 comments
Closed

PSScriptAnalyzer marks unused param but it is used in the script #1924

ktran1005 opened this issue Jun 28, 2023 · 7 comments

Comments

@ktran1005
Copy link

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce
Hello everyone, I am a newbie to PSScriptAnalyzer. I run into a little issue. I run PSScriptAnalyzer to one of my script and here is the warning I received "The parameter "ipAddress" has been declared but not used". I wonder that can this warning consider as false positive since my script did use this parameter by passing it to another script. Any response would be appreciated!

param(
        [Parameter(Mandatory)] [string]$acrName,
        [Parameter(Mandatory)] [string]$ipAddress
)

$networkRuleExists = $false
$existingNetworkRules = ./Get-AcrNetworkRule -acrName $acrName
$existingNetworkRules.ipRules | ForEach-Object -Process { 
    if($_.ipAddressOrRange -eq $ipAddress){ 
        Write-Output "NetworkRule already exists."; 
        $networkRuleExists = $true;
        if(!$networkRuleExists)
            {
                az acr network-rule add --name $acrName --ip-address $ipAddress
                if($LASTEXITCODE -ne 0){Throw "az acr network-rule add returned a $LASTEXITCODE exit code."}
                Start-Sleep -Seconds 120
            } 
        return 
    } 

}

Expected behavior

expected none

Actual behavior

 The parameter 'ipAddress' 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

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
@ghost ghost added the Needs: Triage 🔍 label Jun 28, 2023
@Hrxn
Copy link

Hrxn commented Jul 17, 2023

Well, there's already #1923

But I came here for the same issue, to be honest, I've encountered the same problem.

@JamesWTruher
Copy link
Contributor

yes, this is an known issue in script analyzer because of the "apparent" scope created by the scriptblock in foreach-object. Is definitely a bug, but a tricky one to fix with the current code base. If you re-write as follows, there's no warning

param(
        [Parameter(Mandatory)] [string]$acrName,
        [Parameter(Mandatory)] [string]$ipAddress
)

$networkRuleExists = $false
$existingNetworkRules = ./Get-AcrNetworkRule -acrName $acrName
foreach($rule in $existingNetworkRules.ipRules) {
    if($rule.ipAddressOrRange -eq $ipAddress) {
        Write-Output "NetworkRule already exists.";
        $networkRuleExists = $true;
        if(!$networkRuleExists)
            {
                az acr network-rule add --name $acrName --ip-address $ipAddress
                if($LASTEXITCODE -ne 0){Throw "az acr network-rule add returned a $LASTEXITCODE exit code."}
                Start-Sleep -Seconds 120
            }
        return
    }

}

@Hrxn
Copy link

Hrxn commented Jul 19, 2023

@JamesWTruher Okay, so maybe I am actually referring to a slightly different issue here?

Because in my case, it is definitely not related to the scriptblock by ForEach-Object (and neither Where-Object, for what it's worth..)

As far as I understand it, it is still related to the scope here, though. I get that the ReviewUnusedParameter rule is - based on that description - designed to take scope into consideration, but since the offending switch is in a child scope it is somewhat related, I think?

Anyway, here's a simple example to reproduce:

<#
.SYNOPSIS
	Minimal testcase for demonstration purposes
#>

param(
	[Parameter(Position=0)]
	[string] $ExampleInput = "I am just a simple DEMO string",

	[switch] $Lower,
	[switch] $Upper,
	[switch] $Variant,

	[switch] $DemoSwitch
)

function LocalFunc ([string] $In) {
	if ($DemoSwitch) {
		return $In.Replace('string', 'string, DemoSwitch is SET! SUCCESS!')
	} else {
		return $In.Replace('string', 'string, DemoSwitch is NOT SET! ("default" operation)')
	}
}

if ($Variant) {
	Write-Output (LocalFunc -In $ExampleInput)
}
elseif ($Lower) {
	Write-Output $ExampleInput.ToLower()
}
elseif ($Upper) {
	Write-Output $ExampleInput.ToUpper()
}
else {
	Write-Output $ExampleInput
}

PSScriptAnalyzer complains as following:

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      ps.testcas 14    The parameter 'DemoSwitch' has been declared but not used.
                                                 e.ps1

But the script itself is clearly working as intended.

@microsoft-github-policy-service
Copy link
Contributor

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

@microsoft-github-policy-service
Copy link
Contributor

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

@bergmeister
Copy link
Collaborator

@Hrxn latest example is still a scope issue because that LocalFunc creates a scope/scriptblock

@Hrxn
Copy link

Hrxn commented Sep 17, 2023

True, but this issue is closed now anyways.

The confusion did originally stem from a comment in another closed duplicate issue (#1923) on this very same thing, stating that it's caused by "how PSSA handles scoping for the Where-Object script block".

So, the correct issue for this is now #1472, it seems.
I suggest continuing there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants