How to evaluate powershell script injected from stdin

I want to evaluate the content from StdIn to Powershell, for example:

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

Output: echo 12;

Unfortunately, $input not a string, but a System.Management.Automation.Internal.ObjectReader , which makes iex not so expected ... since this works correctly:

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

Output: 12

+4
source share
1 answer

The following steps will work:

Use script block:

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

Or use single quotes to avoid string interpolation:

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

therefore, the variable $ input will not be expanded, and the string '$ input' will be passed to iex.

Both of them give me "12".

+4
source

All Articles