次の方法で共有


about_Functions_OutputTypeAttribute

簡単な説明

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

詳細な説明

OutputType 属性には、関数から返される .NET 型のオブジェクトが一覧表示されます。 省略可能な ParameterSetName パラメーターを使用すると、パラメーター セットごとに異なる出力の種類を一覧表示できます。

OutputType 属性は、単純関数と高度な関数でサポートされています。 CmdletBinding 属性とは無関係です。

OutputType 属性は、 コマンドレットが返すSystem.Management.Automation.FunctionInfo オブジェクトのGet-Command プロパティの値を提供します。

OutputType 属性値は、ドキュメントのメモにすぎません。 関数コードから派生したり、実際の関数の出力と比較したりしていません。 そのため、値が不正確になる可能性があります。

構文

関数の 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 を持つ関数を作成する

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 にすることができます。 出力が .NET 型 ( WMI オブジェクトやオブジェクトの書式設定されたビューなど) でない場合は、null 値を使用します。

関連項目