8

我想在 PowerShell 中评估来自 StdIn 的内容,如下所示:

echo "echo 12;" | powershell -noprofile -noninteractive -command "$input | iex"

输出:

echo 12;

不幸的是,$input不是一个字符串,而是一个System.Management.Automation.Internal.ObjectReader,它iex不能按预期工作......因为这个工作正常:

powershell -noprofile -noninteractive -command "$command = \"echo 12;\"; $command | iex"

输出:

12
4

2 回答 2

6

以下将起作用:

使用脚本块:

echo "echo 12;" | powershell -noprofile -noninteractive -command { $input | iex }

或者使用单引号来避免字符串插值:

 echo "echo 12;" | powershell -noprofile -noninteractive -command '$input | iex'

所以 $input 变量没有被扩展,字符串 '$input' 被传递给 iex。

这两个都给我“12”。

于 2012-12-14T11:32:07.997 回答
1

只需-用作源文件“名称”:

echo "echo 12;" | powershell -noprofile -noninteractive -
于 2021-04-21T05:28:05.387 回答