我经常使用与此类似的构造(为简洁起见,使用别名):
gci -ad | %{$_ | gci}
效果很好。但是当我试图帮助这个论坛上的其他用户时,我发现以下内容doesn't work:
gci -ad | %{$_.Parent | gci}
每次迭代都会抛出以下错误:
gci : Cannot find path 'C:\Users\keith\Documents\Documents' because it does not exist.
At line:1 char:25
+ gci -ad | %{$_.Parent | gci}
+ ~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\keith\Documents\Documents:Stri
ng) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemC
ommand
虽然:
gci -ad | %{$_.GetType() -eq $_.Parent.GetType()}
产生一屏满True
.
我很确定它与参数绑定有关,但想了解明显的不一致......
查看所发生情况的最简单方法是模拟与测试函数的绑定,但问题是,正如您可能在 .NET Framework 中知道的那样,当DirectoryInfo
实例被强制转换为字符串,结果将是.Name
属性值与 .NET 不同,在 .NET 中,强制转换会导致.FullName
财产。
由于通过调用返回的对象.Parent
财产不具有 ETS 财产.PSPath
,输入对象将被绑定Path
参数并强制为字符串。
假设您有两个版本的 PowerShell,您可以尝试以下操作来查看差异:
function Test-Binding {
[CmdletBinding(DefaultParameterSetName='Items')]
param(
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]
${Path},
[Parameter(ParameterSetName='LiteralItems', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath')]
[string[]]
${LiteralPath}
)
process {
$PSBoundParameters
$PSBoundParameters.Clear()
}
}
(Get-Item .), (Get-Item .).Parent | Test-Binding
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)