如何在 Visual Studio Code 的 PowerShell 扩展中禁用 PSScriptAnalyzer 的 PSAvoidUsingCmdletAliases

Sim*_*onS 11 powershell visual-studio-code

我正在使用Visual Studio Code编写我的 PowerShell 脚本。

我已经安装了ms-vscode.powershellVisual Studio Code的PowerShell 扩展。

每当我在脚本中使用别名时,它都会PSScriptAnalyzer告诉我使用完整的 CmdLet 名称。这有点烦人,因为它还用绿色曲线标记所有别名。

我怎样才能禁用它?

see_screenshot_of_the_problem

Sim*_*onS 16

有三种方法可以做到这一点。

选项 1 - 使用搜索功能

  1. 在 Visual Studio Code 中按 F1 以显示搜索栏
  2. >PowerShell: Select PS然后选择PowerShell: Select PSScriptAnalyzer Rules
  3. 取消勾选 PSAvoidUsingCmdletAliases
  4. 点击 Confirm

图片:

在此处输入图片说明

选项 2 - 完全禁用 ScriptAnalysis

  1. 单击 Visual Studio Code 左下角的齿轮图标
  2. 点击设置
  3. 点击右上角的{}符号
  4. 添加"powershell.scriptAnalysis.enable": false到右侧的用户设置中(请参见下面的屏幕截图)。
  5. 通过点击保存您的用户设置 CTRL + S

截屏:

在此处输入图片说明

您的脚本分析器现已禁用,不会再抱怨别名。

选项 3 - 创建设置文件并仅禁用别名信息

  1. 在您的文件系统上创建一个 .psd1 文件。将下面的模板复制到此文件中并保存。
  2. 如选项 2 第 1 至 3 点所述,转到 VSCode 中的 UserSettings。
  3. 添加"powershell.scriptAnalysis.settingsPath": "C:\\foo\\bar\\FileName.psd1"并保存

这是它的图片:

在此处输入图片说明

模板(取自https://github.com/PowerShell/vscode-powershell/blob/develop/examples/PSScriptAnalyzerSettings.psd1):

@{
    # 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 defualt rules.
    IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
                     'PSMisleadingBacktick',
                     'PSMissingModuleManifestField',
                     'PSReservedCmdletChar',
                     'PSReservedParams',
                     'PSShouldProcess',
                     'PSUseApprovedVerbs',
                     'PSUseDeclaredVarsMoreThanAssigments')

    # 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 = @('PSAvoidUsingAliases','PSAvoidUsingWriteHost')
}
Run Code Online (Sandbox Code Playgroud)

  • 这些工作,但不是 100%。如果您关闭并重新打开 VSCode,#1 不会持续存在。#2 完全禁用 ScriptAnalysis,它禁用所有规则 - 其中一些很有用。#3 有效,但意味着您不能再使用方法 #1 轻松调整规则。有没有人找到一种类似于 #1 但在关闭/重新打开 VSCode 时仍然存在的方法? (2认同)