関数 OutputTypeAttribute について
概要
関数が返すオブジェクトの種類を報告する属性について説明します。
詳細説明
OutputType 属性は、関数が返すオブジェクトの .NET 型を一覧表示します。 省略可能な ParameterSetName パラメーターを使用すると、パラメーター セットごとに異なる出力の種類を一覧表示できます。
OutputType 属性は、単純関数と高度な関数でサポートされています。 これは、CmdletBinding 属性とは独立しています。
OutputType 属性は、Get-Command コマンドレットが返す System.Management.Automation.FunctionInfo オブジェクトの OutputType プロパティの値を提供します。
OutputType 属性値は、ドキュメントのメモにすぎません。 関数コードから派生したり、実際の関数出力と比較したりしません。 そのため、値が不正確になる可能性があります。
SYNTAX
関数の 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>")]
Param ステートメントの前にある属性リストに OutputType 属性ステートメントを配置します。
次の例は、単純な関数での 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>
}
例
次の関数では、OutputType 属性を使用して、文字列値が返されることを示します。
function Send-Greeting
{
[OutputType([String])]
Param ($Name)
"Hello, $Name"
}
結果の出力型プロパティを表示するには、Get-Command コマンドレットを使用します。
PS C:> (Get-Command Send-Greeting).OutputType
Name Type
---- ----
System.String System.String
次の高度な関数では、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>
}
次の例では、出力型プロパティの値が不正確な場合でも、OutputType 属性の値が表示されることを示します。
Get-Time 関数は、DateTime オブジェクトの短い形式の時刻を含む文字列を返します。 ただし、OutputType 属性は System.DateTime オブジェクトを返すという報告を行います。
function Get-Time
{
[OutputType([DateTime])]
Param (
[parameter(Mandatory=$true)]
[Datetime]$DateTime
)
$DateTime.ToShortTimeString()
}
Get-Type メソッドは、関数から文字列が返されることを確認します。
PS C:> (Get-Time -DateTime (Get-Date)).Gettype().FullName
System.String
ただし、OutputType 属性から値を取得する OutputType プロパティは、関数が DateTime オブジェクトを返すという報告を行います。
PS C:> (Get-Command Get-Time).OutputType
Name Type
---- ----
System.DateTime System.DateTime
注
FunctionInfo オブジェクトの OutputType プロパティの値は System.Management.Automation.PSTypeName オブジェクトの配列であり、それぞれに Name プロパティと Type プロパティがあります。
各出力の種類の名前のみを取得するには、次の形式のコマンドを使用します。
(Get-Command Get-Time).OutputType | ForEach {$_.Name}
OutputType プロパティの値は null にすることができます。 WMI オブジェクトやオブジェクトの書式設定されたビューなど、出力が .NET 型ではない場合は null 値を使用します。
関連項目
about_Functions_Advanced_Methods