UseCompatibleTypes

重大度レベル: 警告

説明

このルールは、対象となる 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/PSCompatibilityCollector/profiles 下にあります ( $PSScriptRoot ここでは を含む PSScriptAnalyzer.psd1ディレクトリを参照します)。

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

構成設定

構成キー 説明 指定可能な値 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
IgnoreTypes スクリプトの互換性を無視する型または型アクセラレータの完全な名前 string[]: 無視する型の名前 いいえ (既定値: @()) @('System.Collections.ArrayList','string')

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

@{
    Rules = @{
        PSUseCompatibleTypes = @{
            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 types to not check like this, which will also ignore methods and members on it:
            IgnoreTypes = @(
                'System.IO.Compression.ZipFile'
            )
        }
    }
}

または、次のように設定オブジェクトを指定することもできます。

PS> $settings = @{
      Rules = @{
        PSUseCompatibleTypes = @{
          Enable = $true
          TargetProfiles = @('win-48_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework')
        }
      }
}
PS> Invoke-ScriptAnalyzer -Settings $settings -ScriptDefinition '[System.Management.Automation.SemanticVersion]'1.18.0-rc1''

RuleName                Severity     ScriptName Line  Message
--------                --------     ---------- ----  -------
PSUseCompatibleTypes    Warning                 1     The type 'System.Management.Automation.SemanticVersion' is
                                                      not available by default in PowerShell version
                                                      '5.1.17763.316' on platform 'Microsoft Windows 10 Pro'

抑制

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

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

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

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleTypes', 'System.Management.Automation.Security.SystemPolicy')]

また、型メンバーに対してのみ抑制されます。

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', 'System.Management.Automation.LanguagePrimitives/ConvertTypeNameToPSTypeName')]