注意:这个答案是对Doug Maurer's helpful answer 的补充,它提供了一个有效的解决方案(对于没有空格的文件名)。
PowerShell 解析 未引用 复合标记(例如 $($args[0])+$($args[1]))的方式有一个微妙之处(复合标记是指直接连接的不同语法结构):
$($args[0])+$($args[1]) 导致 两个 参数[1] - 尽管手头有特定的命令(cmd.exe 的内部 @ 987654327@ 命令)发生 不是 是一个问题:
为避免此问题,请将整个复合标记包含在"..." 中,以便可以预见地将其视为expandable string。
结果:
天真地应用于您的命令(请参阅下面更好的解决方案):
# Note: See better solution below.
function join-file {
# Note the "..." around the first argument, and the absence of quoting
# and $(...) around the second.
cmd /c copy /b "$($args[0])+$($args[1])" $args[2]
}
但是,如果$args[0] 或$($args[1]) 包含空格,copy 命令会发生故障;因此,将文件名和+ 作为单独的 参数传递会更加稳健,copy 也支持:
function join-file {
# Pass the arguments individually, which obviates the need for quoting
# and $(...) altogether:
cmd /c copy /b $args[0] + $args[1] $args[2]
}
[1] 您可以按如下方式验证这一点:$arr='foo', 'bar'; cmd /c echo $($arr[0])+$($arr[1]),产生:foo +bar(注意空格)。