特殊変数(2)
以下のページで特殊変数について詳しく書かれています。
Special Variables
「$args」
「$args」はfunctionやScriptBlockの引数配列を格納します。
ScriptBlockの引数を表示する
MSH C:\> $a = {write-host $args[0] $args[1]} MSH C:\> &$a 1 2 3 4 1 2
「$error」
「$error」はエラー発生時のオブジェクトを格納します。
エラー内容を表示する
MSH C:\> ls z:\ get-childitem : Cannot find drive. A drive with name 'z' does not exist. At line:1 char:3 + ls <<<< z:\ MSH C:\> $error get-childitem : Cannot find drive. A drive with name 'z' does not exist. At line:1 char:3 + ls <<<< z:\
「$foreach」
「$foreach」はMSDNによると
Refers to the enumerator in a foreach loop.
とありますが、よく分かりません。
どう利用するのでしょう?
「$input」
「$input」はパイプを通してfunctionやScriptBlockに渡されたオブジェクトを格納します。
プロセス名を列挙する
MSH C:\> function pName >> { >> foreach($process In $input){$process.name} >> } >> MSH C:\> get-process | pName