PowerShell / PowerShell/PSScriptAnalyzer

Rule request: Warning if `Set`/`Get-Variable` is invoked with only the basic `-Name`/`-Value` parameters

未关闭
#1,706 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Area - Rules Issue - New Rule Up-for-Grabs
主要语言
C#
星标
2.2k
派生
414
平均合并
13 小时 1 分钟
30 天内合并 PR
2

描述

Referring to StackOverflow questions along with: How do I automaticaly create and use variable names?.

The cmdlets:

Could completely mislead a novice PowerShell scripter in a wrong programming direction.

The general learning process of a scripter/programmer is playing with variables. Than it gets extended to list of variables like $Var1 = 'One', $Var2 = 'Two', .... The list extends further and they understand they need to automate this somehow but finding guidance for this is quiet a leap and if they ask for guidance at e.g. StackOverflow they often get a direct answer/comment in the direction of using one of these <verb>-Variable cmdlets which is not a good practice for these users and what they atempt to do or learn.

Afaik, there is no good reason to use one of these <verb>-Variable cmdlets which just -Name and -Value parameters.

e.g. Get-Variable MyVar can be replaced by $MyVar and Set-Variable MyVar 'Test' can be replaced by $MyVar = 'Test'

The only other reason to use just the -Name and -Value parameters is to create dynamic variable names (variable indirection) like:

Set-Variable "Var$i" 'Test'

Which is to my opinion a bad practice as they will be added to the same dictionary as the rest of the parameters and one might easily overwrite other general variables and lose the oversight.

Summary of the new feature

When it comes to creating a dynamic variables, a separated dictionary (in the form of a hashtable) should be created and a warning/hint could be given in that direction.

Visa verse, advanced users that know what they are doing, might simply avoid the warning by adding a specific (e.g. -Scope Script) parameter.

What is the latest version of PSScriptAnalyzer at the point of writing
1.19.1

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看现有的 PSScriptAnalyzer 规则实现和测试,然后跟踪对 Get-Variable、Set-Variable 和 New-Variable 的调用是如何被分析的。定义仅使用基本名称/值参数的调用的警告条件,以及显式作用域参数的豁免条件;当规则及其测试覆盖这些情况时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
powershell
领域
tooling
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。