powershell:如何ls dirA,dirB

编程入门 行业动态 更新时间:2024-10-27 03:31:32
powershell:如何ls dirA,dirB -rec |(powershell: how to ls dirA,dirB -rec | group FileName,VersionInfo.ProductVersion | ft -auto …?)

看看我在那里做什么? 当然,它不起作用。

我想在几个目录中获得一个文件名和版本信息(产品名称,版本)的表格,以及重复的计数。

一个问题是我似乎无法触及group-object cmdlet的结果,因此以下内容也不起作用:

ls VisitRecord-1/Release,Dispo-1/Release -rec ` | % {@{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion } ` | New-HashObject} ` | group FileName,ProductName,ProductVersion ` | % {@{Count=$_.Count; FileName=$_.Group.FileName}}

因为$ _。Group.FileName似乎返回null(或空字符串)。

我错过了什么? 有任何想法吗? (我正在运行PowerShell的第2版。)

谢谢。

See what I'm trying to do there? Naturally, it doesn't work.

I want to get a table of filenames and version info (product name, version) in a couple of directories, along with counts of duplicates.

One problem is I can't seem to reach inside the results of the group-object cmdlet, so the following also doesn't work:

ls VisitRecord-1/Release,Dispo-1/Release -rec ` | % {@{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion } ` | New-HashObject} ` | group FileName,ProductName,ProductVersion ` | % {@{Count=$_.Count; FileName=$_.Group.FileName}}

because $_.Group.FileName seems to return null (or empty string).

What am I missing? Any ideas? (I'm running version 2 of PowerShell.)

Thanks.

最满意答案

TL; DR:我将离开原来的答案,因为它显示的解决方法以及随后的讨论有助于了解真正发生的事情。 然而,最佳解决方案是最终的。


原始答案 (有效,但不是最好的方法)

这似乎是PowerShell 2.0中的一个错误。 我在3.0中尝试了你的代码并且它工作正常(除了我使用New-Object -Property而不是| New-HashObject ,因为我没有安装该扩展,这对我来说似乎是多余的)。

由于某些奇怪的原因,在2.0中,您无法读取Group-Object返回的GroupInfo对象的任何NoteProperty属性。 但是,如果使用Select-Object ,它可以工作。 用这个替换你的最后一行:

| %{@{Count=$_.Count; FileName=$_.Group | select -expand FileName}}

在旁注中,正如我上面提到的,我不确定我是否看到了New-HashObject扩展的值。 这也适用,仅使用本机PowerShell(作为代码的第二行和第三行的替代):

| %{New-Object PSObject -Property @{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion} `

UPDATE

上面的解决方法有效,但出于错误的原因。 我想$_.Group.FileName是为了尝试读取GroupInfo对象的NoteProperty ,但是在读完Keith Hill的注释之后我意识到它是$ _这是GroupInfo对象,而$ _。Group是该对象的属性是一个集合。 OP的代码在PowerShell 3.0中工作的原因是集合只有一个对象,因此枚举集合的单个属性( FileName )会返回单个字符串而不是数组。

然而,它是一个集合的事实实际上使解决方案更简单。 只需在.Group之后添加[0]即可检索集合中的第一个(也是唯一的)对象:

| % {@{Count=$_.Count; FileName=$_.Group[0].FileName}}

TL;DR: I'm leaving my original answer, because the workaround it shows and the ensuing discussion help understand what's really going on. However, the optimal solution is at the very end.


ORIGINAL ANSWER (which works but isn't the best way to do it)

This seems to be a bug in PowerShell 2.0. I tried your code in 3.0 and it worked fine (except I used New-Object -Property instead of | New-HashObject, because I don't have that extension installed and it seems superfluous to me).

For some odd reason, in 2.0, you can't read any of the NoteProperty properties of the GroupInfo objects returned by Group-Object. It works if you use Select-Object, though. Replace your last line with this:

| %{@{Count=$_.Count; FileName=$_.Group | select -expand FileName}}

On a sidenote, as I mentioned above, I'm not sure I see the value of the New-HashObject extension. This works just as well, using only native PowerShell (as a replacement for the second and third lines of your code):

| %{New-Object PSObject -Property @{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion} `

UPDATE

The workaround above works, but for the wrong reason. I thought of $_.Group.FileName as an attempt to read a NoteProperty of a GroupInfo object, but after reading Keith Hill's comment I realized that it's $_ that's the GroupInfo object, and $_.Group is a property of that object which is a collection. The reason the OP's code works in PowerShell 3.0 is that the collection has only one object, so enumerating a single property of the collection (FileName) returns a single string rather than an array.

However, the fact that it's a collection actually makes the solution simpler. Just add a [0] after .Group to retrieve the first (and only) object in the collection:

| % {@{Count=$_.Count; FileName=$_.Group[0].FileName}}

更多推荐

本文发布于:2023-07-20 12:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ls   powershell   dirB   dirA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!