【问题标题】:Powershell - "Expressions are only allowed as the first element of a pipeline"Powershell - “表达式只允许作为管道的第一个元素”
【发布时间】:2014-05-26 21:24:41
【问题描述】:

谁能告诉我在以下情况下如何避免这个错误?

$codegenDir = "Z:\Desktop\Song-Renamer"
$PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json

让我完全困惑的是,如果简单地省略 $codegenDir(见下文),代码就会正确运行。我“认为”我理解将表达式放在首位的概念(在管道中的其他项目之前。但我不确定如何重新排列/拆分此代码,因此有问题的表达式Codegen.exe 外部命令行 是管道中的第一项(并且仍然能够通过管道将数据传递给它)。

$PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | .\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json

理想情况下,最好使用尽可能少的代码来做到这一点。

【问题讨论】:

    标签: powershell powershell-3.0 powershell-4.0


    【解决方案1】:

    试一试(唯一的区别是 &):

    $PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | & $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json
    

    这是a link 到一篇关于以不同方式在 powershell 中执行命令的技术网文章。

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 2014-09-24
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2017-06-25
      • 2021-10-19
      相关资源
      最近更新 更多