ja-JP/about_Functions_OutputTypeAttribute.help.txt

TOPIC
    about_Functions_OutputTypeAttribute

簡単な説明
    関数が返すオブジェクトの型を報告する属性について説明します。

詳細な説明
    OutputType 属性は、関数が返すオブジェクトの .NET 型を一覧表示します。
    オプションの ParameterSetName パラメーターを使用して、各パラメーターセ
    ットに対して異なる出力型を一覧表示できます。

    OutputType 属性は、単純関数とアドバンス関数の両方でサポートされていま
    す。これは CmdletBinding 属性とは独立しています。OutputType 属性は、
    Get-Command コマンドレットが返す
    System.Management.Automation.FunctionInfo オブジェクトの OutputType プロ
    パティの値を提供します。

    注: OutputType 属性の値は、関数のコードから導出されたり、実際の関数の出
    力と比較されたりするものではありません。そのため、値が不正確な場合があ
    ります。

    OutputType 属性は、Command | Select <Tab> や Command | Where <Tab> のよ
    うな状況のタブ補完結果にも情報を提供します。

構文
    関数の OutputType 属性の構文は次のとおりです。

        [OutputType([<TypeLiteral>], ParameterSetName="<Name>")]
        [OutputType("<TypeNameString>", ParameterSetName="<Name>")]

    ParameterSetName パラメーターはオプションです。

    OutputType 属性に複数の型を一覧表示できます。

        [OutputType([<Type1>],[<Type2>],[<Type3>])]

    ParameterSetName パラメーターを使用して、異なるパラメーターセットが異な
    る型を返すことを示すことができます。

        [OutputType([<Type1>], ParameterSetName=("<Set1>","<Set2>"))]
        [OutputType([<Type2>], ParameterSetName="<Set3>")]

    OutputType 属性のステートメントは、param ステートメントの前にある属性リ
    ストに配置します。

    次の例は、単純関数での OutputType 属性の配置を示しています。

        function SimpleFunction2
        {
          [OutputType([<Type>])]
          param ($Parameter1)

          <function body>
        }

    次の例は、アドバンス関数での OutputType 属性の配置を示しています。

        function AdvancedFunction1
        {
          [OutputType([<Type>])]
          param (
            [Parameter(Mandatory=$true)]
            [string[]]
            $Parameter1
          )

          <function body>
        }

        function AdvancedFunction2
        {
          [CmdletBinding(SupportsShouldProcess=<Boolean>)]
          [OutputType([<Type>])]
          param (
            [Parameter(Mandatory=$true)]
            [string[]]
            $Parameter1
          )

          <function body>
        }


    例 1: OutputType が String の関数を作成する

        function Send-Greeting
        {
          [OutputType([string])]
          param ($Name)

          "Hello, $Name"
        }

    結果の出力型プロパティを表示するには、Get-Command コマンドレットを使用
    します。

        (Get-Command Send-Greeting).OutputType

        Name Type
        ---- ----
        System.String System.String

    例 2: OutputType 属性を使用して動的な出力型を示す

    次のアドバンス関数は、OutputType 属性を使用して、関数コマンドで使用され
    るパラメーターセットに応じて関数が異なる型を返すことを示します。

        function Get-User
        {
          [CmdletBinding(DefaultParameterSetName="ID")]

          [OutputType("System.Int32", ParameterSetName="ID")]
          [OutputType([string], ParameterSetName="Name")]

          param (
            [Parameter(Mandatory=$true, ParameterSetName="ID")]
            [int]
            $UserID,

            [Parameter(Mandatory=$true, ParameterSetName="Name")]
            [string[]]
            $UserName
          )

          <function body>
        }

    例 3: 実際の出力が OutputType と異なる場合を示す

    次の例は、OutputType 属性の値が不正確であっても、出力型プロパティの値が
    その値を表示することを示しています。

    Get-Time 関数は、任意の DateTime オブジェクトの時刻の短い形式を含む文字
    列を返します。しかし、OutputType 属性は、System.DateTime オブジェクトを
    返すと報告します。

        function Get-Time
        {
          [OutputType([datetime])]
          param (
            [Parameter(Mandatory=$true)]
            [datetime]$DateTime
          )

          $DateTime.ToShortTimeString()
        }

    GetType() メソッドは、関数が文字列を返すことを確認します。

        (Get-Time -DateTime (Get-Date)).GetType().FullName

        System.String

    しかし、OutputType 属性から値を取得する OutputType プロパティは、関数が
    DateTime オブジェクトを返すと報告します。

        (Get-Command Get-Time).OutputType

        Name Type
        ---- ----
        System.DateTime System.DateTime

    例 4: 出力を持つべきでない関数

    次の例は、アクションを実行するが何も返すべきでないカスタム関数を示してい
    ます。

        function Invoke-Notepad
        {
          [OutputType([System.Void])]
          param ()
          & notepad.exe | Out-Null
        }

注釈
    FunctionInfo オブジェクトの OutputType プロパティの値は、
    System.Management.Automation.PSTypeName オブジェクトの配列であり、それぞ
    れが Name および Type プロパティを持ちます。

    各出力型の名前のみを取得するには、次の形式のコマンドを使用します。

        (Get-Command Get-Date).OutputType | Select-Object -ExpandProperty Name

    またはその短い版。

        (Get-Command Get-Date).OutputType.Name

    OutputType プロパティの値は null にできます。関数が Success ストリームに
    出力を書き込まない場合は null 値を使用します。関数が出力を書き込むが型が
    分からない場合は、System.Object を使用します。

関連項目
    about_Functions
    about_Functions_Advanced
    about_Functions_Advanced_Methods
    about_Functions_Advanced_Parameters
    about_Functions_CmdletBindingAttribute

----
原文: PowerShell-Docs (CC BY 4.0) の翻訳 / PSHelpJaJP