Skip to content

Warning about unused PSNativeCommandArgumentPassing preference variable #2092

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

Closed
0xfeeddeadbeef opened this issue Apr 4, 2025 · 2 comments · Fixed by #2093
Closed

Warning about unused PSNativeCommandArgumentPassing preference variable #2092

0xfeeddeadbeef opened this issue Apr 4, 2025 · 2 comments · Fixed by #2093

Comments

@0xfeeddeadbeef
Copy link

Steps to reproduce

Save this script to a file test.ps1:

$PSNativeCommandArgumentPassing = 'Legacy'

'Hello, World!'

Call Invoke-ScriptAnalyzer cmdlet:

Invoke-ScriptAnalyzer -Path .\test.ps1

Expected behavior

Empty output; no warnings.

$PSNativeCommandArgumentPassing is a preference variable.

I'm aware that I can suppress this warnings by applying [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','')] attribute, but it is wrong, because I still want to receive warnings about other unused variables.

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      test.ps1   1     The variable 'PSNativeCommandArgumentPassing' is assigned
s                                                                 but never used.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Microsoft Windows 10.0.20348
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.24.0
@SamErde
Copy link

SamErde commented Apr 4, 2025

Yep. It's the little things like this that sting. I sometimes work around it by adding a line like [void]$PSNativeCommandArgumentPassing or Write-Verbose $PSNativeCommandArgumentPassing but you're right that it shouldn't be necessary for preference variables. Does PSScriptAnalyzer maintain a list of ignored variable names for the PSUseDeclaredVarsMoreThanAssignment test?

@bergmeister
Copy link
Collaborator

We have a list of excluded special variables, it seems that variable is new and needs to be added:

internal static readonly string[] OtherInitializedVariables = new string[]

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

Successfully merging a pull request may close this issue.

3 participants