简体   繁体   English

Powershell通过管道传递路径时的连接路径错误

[英]Powershell's Join-Path Errs when Passing Path via Pipeline

Join-Path allegedly accepts the Path parameter from the pipeline. 据称, Join-Path接受来自管道的Path参数。

This suggests that the two functions below should both work alike: 这表明下面的两个函数应该都是相同的:

join-path 'c:\temp' 'x'     #returns c:\temp\x
'c:\temp' | join-path 'x'   #throws an error

However the second call (ie using passing the Path parameter by value to the pipeline) gives the below error: 但是第二次调用(即使用Path参数按值传递给管道)会产生以下错误:

join-path : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. join-path:输入对象不能绑定到命令的任何参数,因为该命令不接受管道输入或输入及其属性与接受管道输入的任何参数都不匹配。 At line:1 char:13 + 'c:\\temp' | 在行:1 char:13 +'c:\\ temp'| join-path 'x' + ~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (c:\\temp:String) [Join-Path], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.JoinPathCommand join-path'x'+ ~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(c:\\ temp:String)[Join-Path],ParameterBindingException + FullyQualifiedErrorId:InputObjectNotBound,Microsoft.PowerShell.Commands .JoinPathCommand

NB: Since path may be an array I also tried [array]('c:\\temp') | join-path 'x' 注意:由于path可能是一个数组,我也试过[array]('c:\\temp') | join-path 'x' [array]('c:\\temp') | join-path 'x' ; [array]('c:\\temp') | join-path 'x' ; but this made no difference. 但这并没有什么区别。

Have I misunderstood something, or is this a bug in PowerShell? 我是否误解了某些内容,或者这是PowerShell中的错误?

In your first example, an expression join-path 'c:\\temp' 'x' is interpreted by PowerShell as Join-Path -Path 'c:\\temp' -ChildPath 'x' (because the names for positional parameters Path and ChildPath are optional). 在您的第一个示例中,表达式join-path 'c:\\temp' 'x' Join-Path -Path 'c:\\temp' -ChildPath 'x'被PowerShell解释为Join-Path -Path 'c:\\temp' -ChildPath 'x' (因为位置参数PathChildPath的名称)是可选的)。

In your second example, you are passing a pipeline parameter 'c:\\temp' to the command Join-Path -Path 'x' (not the absence of ChildPath parameter). 在第二个示例中,您将管道参数'c:\\temp'传递给命令Join-Path -Path 'x' ChildPath Join-Path -Path 'x' (不是没有ChildPath参数)。 It will not work because Join-Path accepts only Path parameter from the pipeline but you have already defined it. 它不起作用,因为Join-Path只接受来自管道的Path参数,但您已经定义了它。

If you want to bind another , not first parameter, you should write it name explicitly, as in 如果要绑定另一个而不是第一个参数,则应该明确地将其命名为,如

'c:\temp' | Join-Path -ChildPath 'x'
# => 'c:\temp\x'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM