Windpws PowerShell で、任意のファンクションの作成方法について説明します。
Hello,World! を表示する任意のファンクション (hello) を定義
function hello {Write-Host "Hello,World!"}
実行するには、単に hello と入力するだけでよい。
整数値を戻り値とするファンクションを作成
test1 ("A") を実行すると 0 が返り、test1 ("B") を実行すると 1 が返ります。
function test1 ([string]$s1)
{
if ($s1 -eq "A")
{
return 0
}
else
{
return 1
}
}
連想配列を戻り値とするファンクションを作成
test1 "A" "B" を実行すると、引数を連想配列に格納して返ります。
function test1 ([string]$s1, [string]$s2)
{
return @{"s1"=$s1;"s2"=$s2}
}