我只是想知道为什么我在括号中嵌入表达式时会得到两个不同的成员列表,例如: gl -stack
。似乎没有括号,表达式被完全评估,结果立即传递给下一个管道组件。但是使用括号,集合中的单个对象将逐个传递,以便为集合中的对象调用Get-Member
,而不是集合本身。
有关Get-Location -Stack
的示例,请参阅以下PowerShell交互。
提前致谢!
PS C:\temp\loc1> pushd
PS C:\temp\loc1> pushd ..\loc2
PS C:\temp\loc2> gl -stack
Path
----
C:\temp\loc1
C:\temp\loc1
PS C:\temp\loc2> gl -stack | gm
TypeName: System.Management.Automation.PathInfoStack
Name MemberType Definition
---- ---------- ----------
Clear Method System.Void Clear()
Contains Method bool Contains(System.Management.Automation.PathInfo...
CopyTo Method System.Void CopyTo(System.Management.Automation.Pat...
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.Generic.Stack`1+Enumerator[[Syst...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Peek Method System.Management.Automation.PathInfo Peek()
Pop Method System.Management.Automation.PathInfo Pop()
Push Method System.Void Push(System.Management.Automation.PathI...
ToArray Method System.Management.Automation.PathInfo[] ToArray()
ToString Method string ToString()
TrimExcess Method System.Void TrimExcess()
Count Property System.Int32 Count {get;}
Name Property System.String Name {get;}
PS C:\temp\loc2> (gl -stack) | gm
TypeName: System.Management.Automation.PathInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Drive Property System.Management.Automation.PSDriveInfo Drive {get;}
Path Property System.String Path {get;}
Provider Property System.Management.Automation.ProviderInfo Provider {...
ProviderPath Property System.String ProviderPath {get;}
答案 0 :(得分:2)
Get-Location -Stack
会返回PathInfoStack
个对象。该对象派生自实现ICollection的Stack<T>
。将表达式放在()
内时,PowerShell会对该表达式求值。如果结果是一个集合,那么它将被迭代并输出。你可以通过这个简单的函数看到相同的东西:
PS> function GetArray() { ,@(1,2,3) }
PS> GetArray | Foreach {$_.GetType().FullName}
System.Object[]
PS> (GetArray) | Foreach {$_.GetType().FullName}
System.Int32
System.Int32
System.Int32