Powershell - "表达式只允许作为管道的第一个元素"

MKA*_*NET 4 powershell powershell-3.0 powershell-4.0

有人可以告诉我如何在以下情况下避免这个错误?

$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
Run Code Online (Sandbox Code Playgroud)

令我完全困惑的是,如果简单地省略$ 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
Run Code Online (Sandbox Code Playgroud)

理想情况下,使用尽可能少的代码执行此操作会很好.

Hun*_*son 6

给出以下注射(唯一的区别是&):

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

这是一篇关于以不同方式在powershell中执行命令的technet文章的链接.