加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Powershell中的Bash / batch-like子shell

发布时间:2020-12-15 22:36:12 所属栏目:安全 来源:网络整理
导读:在Unix shell中,你可以这样写: ( cmd1 ; cmd2 ) | cmd3 在Windows Batch中,您可以编写此代码 ( cmd1 cmd2 ) | cmd3 在这两种情况下,cmd1和cmd2的输出都传递给stdin上的cmd3. 是否有可能在Powershell中做同样的事情? 我还没有找到允许这种情况的有效语法.我
在Unix shell中,你可以这样写:

( cmd1 ; cmd2 ) | cmd3

在Windows Batch中,您可以编写此代码

( cmd1 & cmd2 ) | cmd3

在这两种情况下,cmd1和cmd2的输出都传递给stdin上的cmd3.

是否有可能在Powershell中做同样的事情?

我还没有找到允许这种情况的有效语法.我希望语句块可以像这样工作,但它没有:

{ cmd1 ; cmd2 ) | cmd3

我可以通过声明一个函数来使它工作:

function f() {
    cmd1
    cmd2
}
f | cmd3

是否有一种语法可以让它在线完成?

解决方法

我认为这有效:

Invoke-Command {cmd1; cmd2} | cmd3

按documentation:

The Invoke-Command cmdlet runs commands on a local or remote computer
and returns all output from the commands,including errors.

You can also use Invoke-Command on a local computer to evaluate or run a string in a script block as a command. Windows PowerShell converts the script block to a command and runs the command immediately in the current scope,instead of just echoing the string at the command line.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读