UseCompatibleCommands

重大度レベル: 警告

説明

このルールは、対象となる PowerShell プラットフォームで使用できないコマンドを識別します。

PowerShell プラットフォームは、次の形式の名前で識別されます。

<os-name>_<os-arch>_<os-version>_<ps-version>_<ps-arch>_<dotnet-version>_<dotnet-edition>

条件:

  • <os-name>: PowerShell が実行されているオペレーティング システムの名前。 Windows では、これには SKU 番号が含まれます。 Linux では、これはディストリビューションの名前です。
  • <os-arch>: オペレーティング システムが実行されているマシン アーキテクチャ (通常 x64は です)。
  • <os-version>: オペレーティング システムの自己報告バージョン (Linux では、これがディストリビューション バージョンです)。
  • <ps-version>: PowerShell バージョン (から $PSVersionTable.PSVersion)。
  • <ps-arch>: PowerShell プロセスのマシン アーキテクチャ。
  • <dotnet-version>: 報告されたバージョンの .NET ランタイム PowerShell が (から System.Environment.Version) で実行されています。
  • <dotnet-edition>: .NET ランタイム フレーバー PowerShell が (現在または core) で実行されていますframework

次に例を示します。

  • win-4_x64_10.0.18312.0_5.1.18312.1000_x64_4.0.30319.42000_frameworkは、x64 用の Windows 10 Enterprise (ビルド 18312) で実行されている PowerShell 5.1 です。
  • win-4_x64_10.0.18312.0_6.1.2_x64_4.0.30319.42000_core は、同じオペレーティング システムで実行されている PowerShell 6.1.2 です。
  • ubuntu_x64_18.04_6.2.0_x64_4.0.30319.42000_core は、Ubuntu 18.04 で実行されている PowerShell 6.2.0 です。

一部のプラットフォームは、PSScriptAnalyzer に JSON ファイルとしてバンドルされています。この方法で、構成のターゲット設定用にという名前が付けられています。

既定でバンドルされているプラットフォームは次のとおりです。

PowerShell バージョン オペレーティング システム id
3.0 Windows Server 2012 win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework
4.0 Windows Server 2012R2 win-8_x64_6.3.9600.0_4.0_x64_4.0.30319.42000_framework
5.1 Windows Server 2016 win-8_x64_10.0.14393.0_5.1.14393.2791_x64_4.0.30319.42000_framework
5.1 Windows Server 2019 win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework
5.1 Windows 10 1809 (RS5) win-48_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework
6.2 Windows Server 2016 win-8_x64_10.0.14393.0_6.2.4_x64_4.0.30319.42000_core
6.2 Windows Server 2019 win-8_x64_10.0.17763.0_6.2.4_x64_4.0.30319.42000_core
6.2 Windows 10 1809 (RS5) win-4_x64_10.0.17763.0_6.2.4_x64_4.0.30319.42000_core
6.2 Ubuntu 18.04 LTS ubuntu_x64_18.04_6.2.4_x64_4.0.30319.42000_core
7.0 Windows Server 2016 win-8_x64_10.0.14393.0_7.0.0_x64_3.1.2_core
7.0 Windows Server 2019 win-8_x64_10.0.17763.0_7.0.0_x64_3.1.2_core
7.0 Windows 10 1809 (RS5) win-4_x64_10.0.17763.0_6.2.4_x64_3.1.2_core
7.0 Ubuntu 18.04 LTS ubuntu_x64_18.04_7.0.0_x64_3.1.2_core

その他のプロファイルは、 GitHub リポジトリにあります。

PSCompatibilityCollector モジュールを使用して、独自のプラットフォーム プロファイルを生成することもできます。

互換性プロファイルの設定では、 の下 TargetProfilesでターゲットにするプラットフォームの一覧が取得されます。 プラットフォームは次のように指定できます。

  • プラットフォーム名 (など ubuntu_x64_18.04_6.1.1_x64_4.0.30319.42000_core) です。この名前は .json 末尾に追加され、既定のプロファイル ディレクトリで検索されます。
  • 既定のプロファイル ディレクトリで を検索するファイル名 (など my_custom_platform.json)。
  • ファイルへの絶対パス (など D:\PowerShellProfiles\TargetMachine.json)。

既定のプロファイル ディレクトリは、 の PSScriptAnalzyer モジュールの $PSScriptRoot/compatibility_profiles 下にあります ( $PSScriptRoot ここでは を含む PSScriptAnalyzer.psd1ディレクトリを参照します)。

互換性分析では、ターゲット プロファイルと 'union' プロファイルの両方に使用されるコマンドを比較します (プロファイル ディレクトリ内の 任意 のプロファイルで使用可能なすべてのコマンドを含みます)。 コマンドが共用体プロファイルに存在しない場合は、ローカルに作成され、無視されるものと見なされます。 それ以外の場合、コマンドが共用体プロファイルに存在するが、ターゲットには存在しない場合、そのターゲットと互換性がないとみなされます。

構成設定

構成キー 説明 指定可能な値 Mandatory
Enable ルールをアクティブにします bool ($true/$false) いいえ (既定値: $false) $true
TargetProfiles 対象となる PowerShell プロファイルの一覧 string[]: プロファイル ファイルまたはプロファイル ディレクトリ内のプロファイル名への絶対パス いいえ (既定値: @()) @('ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core', 'win-48_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework')
ProfileDirPath 名前でプロファイルを検索し、共用体プロファイルの生成に使用する場所 string: 新しいプロファイル dir への絶対パス いいえ (PSScriptAnalyzer モジュールの既定値 compatibility_profiles はディレクトリです) C:\Users\me\Documents\pssaCompatProfiles
IgnoreCommands スクリプト内の の互換性を無視するコマンド string[]: 無視するコマンドの名前 いいえ (既定値: @()) @('Get-ChildItem','Import-Module')

構成例は次のようになります。

@{
    Rules = @{
        PSUseCompatibleCommands = @{
            Enable = $true
            TargetProfiles = @(
                'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core'
                'win-48_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework'
                'MyProfile'
                'another_custom_profile_in_the_profiles_directory.json'
                'D:\My Profiles\profile1.json'
            )
            # You can specify commands to not check like this, which also will ignore its parameters:
            IgnoreCommands = @(
                'Install-Module'
            )
        }
    }
}

抑制

コマンド互換性診断は、他のルールと同様にparam、scriptblock のブロックの 属性を使用して抑制できます。

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', '')]

ルールは、特定のコマンドに対してのみ抑制することもできます。

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', 'Start-Service')]

また、パラメーターに対してのみ抑制されます。

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', 'Import-Module/FullyQualifiedName')]