选择对象-ExpandProperty与Get-ItemPropertyValue

时间:2018-09-14 10:06:50

标签: powershell

关于PowerShell的某些基本知识我很可能不了解。我什至不喜欢编写中等大小的管道,在这种情况下,获取属性会破坏工作流程,因为必须在语句之前加上括号,例如。

(Get-ChildItem ~\.gitconfig).Length

这很乏味。因为Length看起来很像是一种财产,所以人们会认为

Get-ChildItem ~\.gitconfig | Get-ItemPropertyValue -Name Length

会工作。但是,事实并非如此。看一下文件系统PSDrive提供程序返回的System.IO.FileSystemInfo对象的接口,就会发现它没有Length属性。它确实具有FullName属性,因此

Get-ChildItem ~\.gitconfig | Get-ItemPropertyValue -Name FullName

按预期工作。要使用管道检索文件的大小(Length),必须将Select-Object-ExpandProperty一起使用

Get-ChildItem ~\.gitconfig | Select-Object -ExpandProperty Length

如何预先知道是否将.放在对象之后,并通过制表符完成的结果进行迭代(如果条目是对象还是属性)?甚至普通的操作都令人困惑,例如,读取环境变量时会遇到

Get-Item -Path Env:\USERNAME

返回

Name                           Value
----                           -----
USERNAME                       mnagy

如果是项目,Get-ItemPropertyGet-ItemPropertyValue必须在这里扮演角色。由于结果的Name:Value结构,因此可能会对新手产生兴趣,以获取实际的价值

Get-Item -Path Env:\USERNAME | Get-ItemPropertyValue

或实际阅读应如何使用Get-ItemPropertyValue将查询修改为

Get-ItemPropertyValue -Path Env:\ -Name USERNAME

实际上会导致

Get-ItemPropertyValue : Cannot use interface. The IPropertyCmdletProvider interface is not supported by this provider.
At line:1 char:1
+ Get-ItemPropertyValue -Path Env:\ -Name USERNAME
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotImplemented: (:) [Get-ItemPropertyValue], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyValueCommand

整个结构似乎与我完全不一致,而且最令人烦恼,但希望不是设计使然,而是因为我从错误的角度看待它。

0 个答案:

没有答案
相关问题