How to evaluate powershell script inputed from stdin

喜夏-厌秋 提交于 2019-12-21 12:21:23

问题


I want to evaluate the content from StdIn in Powershell, like this:

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

Output: echo 12;

Unfortunately, $input is not a String, but a System.Management.Automation.Internal.ObjectReader, which make iex not working as expected... since this one is working correctly:

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

Output: 12


回答1:


The following would work:

Use a scriptblock:

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

Or use single quotes to avoid the string interpolation:

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

so the $input variable isn't expanded, and the string '$input' is passed to iex.

Both of these give me "12".



来源:https://stackoverflow.com/questions/13876138/how-to-evaluate-powershell-script-inputed-from-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!