Powershell 选择对象-ExpandProperty vs.Get ItemPropertyValue
很可能我对PowerShell有一些基本的不了解。我真的不喜欢写中等大小的管道,因为在这一点上,抓取一个属性会破坏工作流程,因为必须在语句周围放置paren,例如Powershell 选择对象-ExpandProperty vs.Get ItemPropertyValue,powershell,Powershell,很可能我对PowerShell有一些基本的不了解。我真的不喜欢写中等大小的管道,因为在这一点上,抓取一个属性会破坏工作流程,因为必须在语句周围放置paren,例如 (Get-ChildItem ~\.gitconfig).Length 这太乏味了。因为Length看起来非常像一个属性,人们会认为 Get-ChildItem ~\.gitconfig | Get-ItemPropertyValue -Name Length 会有用的。然而,事实并非如此。查看文件系统PSDrive提供程序返回的
(Get-ChildItem ~\.gitconfig).Length
这太乏味了。因为Length
看起来非常像一个属性,人们会认为
Get-ChildItem ~\.gitconfig | Get-ItemPropertyValue -Name Length
会有用的。然而,事实并非如此。查看文件系统PSDrive提供程序返回的System.IO.FileSystemInfo
对象的接口,可以发现它没有Length属性。但它确实有一个全名属性,因此
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 ItemProperty
和Get ItemPropertyValue
必须在这里发挥作用。由于结果的名称:值结构,新来者可能会对获取实际值感兴趣
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
对我来说,整个结构似乎完全不一致,最令人烦恼的是,但希望不是出于设计,而是因为我从错误的角度看待它。对于您的第一个注释:
Length
,对于.hg
directoy,对我来说效果很好(给您提供了其中的文件数):
我倾向于使用get member
来检查哪些支持,哪些不支持
如果我检查它是否有我的目录报告
:
(Get-ChildItem reports) | gm
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
LinkType CodeProperty System.String LinkType{get=GetLinkType;}
Mode CodeProperty System.String Mode{get=Mode;}
Target CodeProperty System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=...
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(s...
Create Method System.IO.FileStream Create()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateText Method System.IO.StreamWriter CreateText()
Decrypt Method void Decrypt()
Delete Method void Delete()
Encrypt Method void Encrypt()
Equals Method bool Equals(System.Object obj)
GetAccessControl Method System.Security.AccessControl.FileSecurity GetAccessControl(), System.Secur...
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys...
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
MoveTo Method void MoveTo(string destFileName)
Open Method System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Op...
OpenRead Method System.IO.FileStream OpenRead()
OpenText Method System.IO.StreamReader OpenText()
OpenWrite Method System.IO.FileStream OpenWrite()
Refresh Method void Refresh()
Replace Method System.IO.FileInfo Replace(string destinationFileName, string destinationBa...
SetAccessControl Method void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity)
ToString Method string ToString()
PSChildName NoteProperty string PSChildName=jv_libgdbs_tests-20180822-Test.xml
PSDrive NoteProperty PSDriveInfo PSDrive=C
PSIsContainer NoteProperty bool PSIsContainer=False
PSParentPath NoteProperty string PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\prg_sdk\stx8-j...
PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\FileSystem::C:\prg_sdk\stx8-jv_swin...
PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
CreationTimeUtc Property datetime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property string DirectoryName {get;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
IsReadOnly Property bool IsReadOnly {get;set;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
LastWriteTime Property datetime LastWriteTime {get;set;}
LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
Length Property long Length {get;}
Name Property string Name {get;}
BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re...
VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVer...
如何预先知道是否放置了一个。在一个对象之后
如果条目是
对象还是属性
您可以使用Get Member
检查它
对于Get Item-Path Env:\USERNAME
,您可以再次检查:
PS C:\> Get-Item -Path Env:\USERNAME | gm
TypeName: System.Collections.DictionaryEntry
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = Key
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
PSDrive NoteProperty PSDriveInfo PSDrive=Env
PSIsContainer NoteProperty bool PSIsContainer=False
PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\Environment::USERNAME
PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\Environment
Key Property System.Object Key {get;set;}
Value Property System.Object Value {get;set;}
现在检查用户名(您可以看到您请求的密钥及其值):
PS C:\> Get-Item -Path Env:\USERNAME | gm
TypeName: System.Collections.DictionaryEntry
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = Key
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
PSDrive NoteProperty PSDriveInfo PSDrive=Env
PSIsContainer NoteProperty bool PSIsContainer=False
PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\Environment::USERNAME
PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\Environment
Key Property System.Object Key {get;set;}
Value Property System.Object Value {get;set;}
PS C:\> (Get-Item -Path Env:\USERNAME).key
USERNAME
PS C:\> (Get-Item -Path Env:\USERNAME).value # my login
gurun