UseCompatibleCmdlets

嚴重性層級:警告

預設狀態:永遠啟用

描述

此規則偵測 PowerShell 版本、版本及作業系統無法提供的 cmdlet。 規則會將腳本中的每個 cmdlet 與隨 PSScriptAnalyzer 附帶的相容性允許清單做比較。 這些檔案位於以下位置: /path/to/PSScriptAnalyzerModule/Settings

每個檔案使用以下命名格式:

<psedition>-<psversion>-<os>.json

Where:

  • <psedition>CoreDesktop
  • <psversion> 是 PowerShell 版本
  • <os>WindowsLinuxLinux-Arm,或 MacOS

設定規則

要檢查你的腳本是否相容於 Windows 上的 PowerShell Core 6.1,請新增以下設定:

@{
    'Rules' = @{
        'PSUseCompatibleCmdlets' = @{
            'compatibility'      = @('core-6.1.0-windows')
        }
    }
}

相容參數接受一個或多個目標識別碼的清單,例如:

  • desktop-2.0-windows
  • desktop-3.0-windows
  • desktop-4.0-windows(取自 Windows Server 2012 R2)
  • desktop-5.1.14393.206-windows
  • core-6.1.0-windows(取自 Windows 10,版本 1803)
  • core-6.1.0-linux (取自 Ubuntu 18.04)
  • core-6.1.0-linux-arm (取自 Raspbian)
  • core-6.1.0-macos

修補過的 PowerShell 版本通常包含相同的 cmdlet 資料,因此此規則主要提供主要與次要版本設定檔。

你也可以用 New-CommandDataFile.ps1建立自訂個人檔案。 將產生的 JSON 檔案放入 Settings 模組的 PSScriptAnalyzer 資料夾中。 然後你可以在 compatibility.

由於 PowerShell 6.0 已終止生命週期,這些 core-6.0.2-* 檔案在 PSScriptAnalyzer 1.18 中被移除。

雖然規則可設定,但規則始終啟用。 請使用以下方法之一以避免使用此規則:

  • 建立自訂規則設定檔,只包含你想要的規則,或排除你不想要的規則。
  • 在程式碼中加入適當的規則抑制屬性,以抑制特定程式碼區塊的規則。 欲了解更多資訊,請參閱使用 PSScriptAnalyzer抑制規則章節。