gpt4 book ai didi

powershell - "(gl -stack)"和 "gl -stack"之间的区别

转载 作者:行者123 更新时间:2023-12-03 03:17:28 28 4
gpt4 key购买 nike

我只是想知道为什么在括号之间嵌入表达式时会得到两个不同的成员列表,例如gl-stack。看起来,如果没有括号,表达式就会被完全求值,并且结果会立即传递到下一个管道组件。但通过括号,集合中的单个对象将被一一传递,以便为集合中的对象而不是集合本身调用 Get-Member。请参阅以下 PowerShell 交互,了解 Get-Location -Stack 的示例。

提前致谢!

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;}

最佳答案

Get-Location -Stack返回 PathInfoStack正如你所看到的那样。该对象源自Stack<T>它实现了 ICollection。当您将表达式放入 () 中时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

关于powershell - "(gl -stack)"和 "gl -stack"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com