PwerShellでの関数は、パイプラインからの値を受け取ることが可能です。
パイプラインからの渡された値は $input という自動変数に格納されます。
まずは、コンソールで下記の通り入力し、pipe-func1 という関数をPowerShellに登録しましょう。
PS > function pipe-func1 >> { >> foreach ($a in $input) >> { >> Write-Host $a >> } >> } >>
この関数は、パイプラインで受け取った値をforeachで1つずつ取り出し表示するという単純な関数です。
以下のようにして、この関数を実験してみてください。
PS > 1..10 | pipe-func1 1 2 3 4 5 6 7 8 9 10
1..10 というのは、1から10までを表します。(範囲演算子(PowerShell 入門)を参照ください)
結果としてこの関数は$inputに1から10までの数値を受け取り、その値を表示します。
もう1つ実験してみましょう。
PS > "apple","banana","orange" | pipe-func1 apple banana orange
こちらは"apple","banana","orange"という3つの値が$inputに渡され、結果その値を表示します。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail