PowerShell 别名、PSScriptAnalyzer 和 VSCode

Yor*_*ubs 6 powershell aliases visual-studio-code psscriptanalyzer

有没有办法抑制PSScriptAnalyzer突出显示alias警告?例如

'rm' is an alias of 'Remove-Item'. Aliases can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.
Run Code Online (Sandbox Code Playgroud)

PowerShell 中的别名非常有用。我有一个简单的规则:我只在脚本中使用合理的内置别名(我忽略奇怪的别名)。为什么?好吧,这些特定别名中的大多数现在已有 13 年历史并且从未改变过(PowerShell 1.0 于 2006 年 11 月 14 日发布)。例如,%orlscd99.99% 的情况下都是可靠的。我认为 99.99% 的可靠性就“足够好”了。可能对所有PowerShell StackOverflow 问题最重复的评论是“注意:不建议在 PowerShell 脚本中使用别名,因为它们可以更改! ”(我经常想知道不推荐?上帝?;-))

然而,PSScriptAnalyzer在 VSCode 中,所有别名都突出显示为问题,因此我当前的 7,000 行脚本有 488 个此类“问题”。有没有办法告诉PSScriptAnalyzer我我喜欢别名,我打算使用别名来获得更加简洁的代码、清晰度以及它们给我带来的大大提高的可读性,所以我不认为它们是问题

小智 2

Mathias 的评论指出要搜索“选择 PSScriptAnalyzer 规则”,但我无法找到该设置(VS 1.58.2、ms-vscode.powershell 2021.6.2)。

我找到的解决方案是更改“脚本分析:设置路径”以指向包含以下代码1的已创建文件,以将某些别名列入白名单。下面我取消了相关部分的注释。

@{
# Only diagnostic records of the specified severity will be generated.
# Uncomment the following line if you only want Errors and Warnings but
# not Information diagnostic records.
#Severity = @('Error','Warning')

# Analyze **only** the following rules. Use IncludeRules when you want
# to invoke only a small subset of the default rules.
IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
                  'PSMisleadingBacktick',
                  'PSMissingModuleManifestField',
                  'PSReservedCmdletChar',
                  'PSReservedParams',
                  'PSShouldProcess',
                  'PSUseApprovedVerbs',
                  'PSAvoidUsingCmdletAliases',
                  'PSUseDeclaredVarsMoreThanAssignments')

# Do not analyze the following rules. Use ExcludeRules when you have
# commented out the IncludeRules settings above and want to include all
# the default rules except for those you exclude below.
# Note: if a rule is in both IncludeRules and ExcludeRules, the rule
# will be excluded.
#ExcludeRules = @('PSAvoidUsingWriteHost')

# You can use rule configuration to configure rules that support it:
Rules = @{
    PSAvoidUsingCmdletAliases = @{
        Whitelist = @("cd")
    }
}
}
Run Code Online (Sandbox Code Playgroud)

[1] https://github.com/PowerShell/vscode-powershell/blob/master/examples/PSScriptAnalyzerSettings.psd1