iex (invoke-expression) と変数を使用して関数を動作させることができません。私が欲しいもの:
function My-Function {
# code that constructs $command
#$command is an arbitrary string that may contain pipes,
#variables, command line paramaters, switchs, quoted strings, THE LOT!
Execute-My-Command-Exactly-As-Though-I-Had-Typed-It-Here $command
}
例えば
function My-Function {
$command = "`$input | select-string hello"
iex $command
}
これを行います( . 上記の関数を含むファイルを含めた後)
PS C:\> echo "hello" | My-Function
PS C:\>
しかし、明らかにこれを行う必要があります:
PS C:\> echo "hello" | My-Function
hello
PS C:\>
さらに、実際のコマンド ラインでは、変数 DO が iex によって取得されているようです。
PS C:\> $hello = "hello"
PS C:\>
PS C:\> iex "`$hello | select-string hello"
hello
PS C:\>
私が考えることができる唯一の回避策は、実際に関数をファイルに書き出すことです. それを含めてから呼び出します。しかし、これはひどいです!優れたシェルスクリプト言語の要点は、このメタ的なことを簡単に実行できることです。
解決:
PowerShell ではできませんが、latkin が役立つヒントを提供しています。