如何评估从标准输入输入的 PowerShell 脚本 [英] How to evaluate PowerShell script inputed from stdin

查看:57
本文介绍了如何评估从标准输入输入的 PowerShell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

输出:

echo 12;

不幸的是,$input 不是一个字符串,而是一个 System.Management.Automation.Internal.ObjectReader,这使得 iex 无法工作正如预期的那样......因为这个工作正常:

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"

输出:

12

推荐答案

以下方法可行:

使用脚本块:

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'

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

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

这两个都给我12".

这篇关于如何评估从标准输入输入的 PowerShell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆