PowerShell / PSScriptAnalyzer

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

foreach Variable Assignment isn't Validated by PSAvoidAssignmentToAutomaticVariable #2020

Open PoshAJ opened 1 month ago

PoshAJ commented 1 month ago

Steps to reproduce

Invoke-ScriptAnalyzer -ScriptDefinition 'foreach ($Host in $foo) {}'

Expected behavior

The Variable 'Host' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.

Actual behavior

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.4.4
PSEdition                      Core
GitCommitId                    7.4.4
OS                             Microsoft Windows 10.0.22631
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
SydneyhSmith commented 4 weeks ago

Thanks this is a tricky one because of scoping--- we will take a look a the PR, thanks for opening