Windows PowerShell 2.0 CTP(5)スクリプトコマンドレット2

スクリプトコマンドレットで、引数を必須指定にする

スクリプトコマンドレットで、引数を必須指定にする「Mandatory」パラメータを利用してみましょう。


以下は、引数に指定した2つの文字列をスペースで区切って表示するスクリプトコマンドレットです。
ただし、1つめの引数は必須指定にしています。

Cmdlet Write-Message
{
    Param ([Mandatory]$message1, $message2)
    Begin{}
    Process{Write-Host "$message1 $message2"}
    End{}
}
実行結果1:引数を2つ指定
PS C:\> Write-Message PowerShell 2.0
PowerShell 2.0
実行結果2:引数を1つ指定
PS C:\> Write-Message PowerShell
PowerShell
実行結果3:引数を指定なし

1つめの引数を指定するように促すメッセージが表示されます。
「Mandatory」パラメータを指定しているからですね。

PS C:\> Write-Message

cmdlet Write-Message at command pipeline position 1
Supply values for the following parameters:
message1: