如何将 Invoke-Expression 的输出通过管道传输到字符串? [英] How to pipe output of Invoke-Expression to string?

查看:47
本文介绍了如何将 Invoke-Expression 的输出通过管道传输到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Powershell 脚本中,我是这样的:

In a Powershell script, I something like this:

Invoke-Expression "& `"C:\Scripts\psftp.exe`" ftp.blah.com"

我想将所有输出、错误等通过管道传输到字符串 $output

I want to pipe all output, errors etc of this to the string $output

我该怎么做?我在最后尝试了 > 以及 $output = ... 但似乎都没有发现错误和排序.

How can I do this? I've tried > at the end as well as $output = ... but neither seem to catch errors and the sort.

推荐答案

试试这个:

$output = Invoke-Expression "C:\Scripts\psftp.exe ftp.blah.com 2>&1"

调用运算符 & 的使用是不必要的,因为在这种情况下引用 exe 路径是不必要的,因为该路径不包含空格.如果路径包含空格,则需要引用它,然后您必须使用调用运算符.也就是说,在这种情况下,我不确定您为什么需要使用 Invoke-Expression.鉴于您的示例,以下内容同样适用.

The use of the call operator & is unnecessary as is quoting the exe path in this case since the path doesn't contain spaces. If the path contained spaces, then it would need to be quoted and then you would have to use the call operator. That said, I'm not sure why you need to use Invoke-Expression at all in this case. The following would work just as well given your example.

$output = C:\Scripts\psftp.exe ftp.blah.com 2>&1

这篇关于如何将 Invoke-Expression 的输出通过管道传输到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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