看到我想做什么了吗?很自然,它不起作用。
我想得到一个表的文件名和版本信息(产品名称,版本)在几个目录,以及计数的重复。
一个问题是,我似乎无法进入组对象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。)
谢谢。
发布于 2014-01-15 17:46:33
TL;DR:我要留下我最初的答案,因为它所显示的解决方法和随后的讨论有助于理解到底发生了什么。然而,最优解就在最后。
原始答案(这很有效,但不是最好的方法)
这似乎是PowerShell 2.0中的一个bug。我在3.0中尝试过您的代码,它运行得很好(除了我使用New-Object -Property而不是| New-HashObject,因为我没有安装那个扩展,这对我来说似乎是多余的)。
由于一些奇怪的原因,在2.0中,您无法读取Group-Object.返回的GroupInfo对象的任何NoteProperty属性不过,如果您使用Select-Object,,它就能工作。将最后一行替换为:
| %{@{Count=$_.Count; FileName=$_.Group | select -expand FileName}}在sidenote上,正如我前面提到的,我不确定我是否看到了New-HashObject扩展的值。使用本机PowerShell (作为代码的第二行和第三行的替代品)也同样有效:
| %{New-Object PSObject -Property @{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion} `更新
以上的解决办法是可行的,但原因是错误的。我认为$_.Group.FileName是试图读取GroupInfo对象的一种尝试,但是在阅读了Keith的评论之后,我意识到是$_才是GroupInfo对象,$_.Group是该对象的一个属性,它是一个集合。OP的代码在PowerShell 3.0中工作的原因是集合只有一个对象,因此枚举集合的单个属性(FileName)返回单个字符串而不是数组。
然而,事实上,它是一个集合,实际上使解决方案更简单。只需在[0]后添加.Group,以检索集合中的第一个(也是唯一)对象:
| % {@{Count=$_.Count; FileName=$_.Group[0].FileName}}发布于 2014-01-15 17:45:16
它的工作原理是-在V4上。您可能需要对V2和以下方面有额外的了解,例如:
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}}}发布于 2014-01-15 18:21:17
前两个答案很好,非常感谢。希望我能把它们都记为答案。回答我自己的问题,这样我就可以删除一些代码,我认为我不能在评论中这样做。
我的解决方案(耶,吃午饭和修补):
ls VisitRecord-1/Release,Dispo-1/Release -rec `
| % {@{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion }} `
| New-HashObject `
| group FileName,ProductName,ProductVersion `
| % {$grp = $_.Name -split ', '; @{Count=$_.Count; FileName=$grp[0]; ProductName=$grp[1]; ProductVersion=$grp[2]}} `
| New-HashObject `
| Select Count,FileName,ProductName,ProductVersion `
| sort ProductName,FileName `
| ft -auto看起来还是有点过火了,但我还是认为这是错误的解决办法。
我喜欢PSCX,但我确实对什么是本地PS和什么是PSCX感到困惑。我认为的一个成功之处在于它可以在其输入上接受一个哈希表流,并生成一个对象流。可能的性能增益?
总之,很高兴发现它是PS2.0中的一个bug。我想这是一个很好的理由来升级。:)
安装PS3.0后的编辑
重写命令行:
ls VisitRecord-1/Release,Dispo-1/Release -rec `
| % {@{FileName=$_.Name; `
ProductName=$_.VersionInfo.ProductName; `
ProductVersion=$_.VersionInfo.ProductVersion }} `
| New-HashObject `
| group FileName,ProductName,ProductVersion `
| % {@{Count=$_.Count; `
GroupCount=$_.Group.Count; `
FileName=$_.Group[0].FileName; `
ProductName=$_.Group[0].ProductName; `
ProductVersion=$_.Group[0].ProductVersion}} `
| New-HashObject `
| select Count,GroupCount,FileName,ProductName,ProductVersion `
| sort ProductName,FileName `
| ft -auto添加GroupCount以进行演示,并添加到组引用中。也许这会使它在PS2.0下工作,但是现在已经没有回头了(对我来说)。:)
(编辑:毕竟,上面的内容在PS2.0下确实有效。)
比你想知道的更多关于组对象的信息:
ls VisitRecord-1/Release,Dispo-1/Release -rec `
| % {@{FileName=$_.Name; ProductName=$_.VersionInfo.ProductName; ProductVersion=$_.VersionInfo.ProductVersion }} `
| New-HashObject `
| group FileName,ProductName,ProductVersion `
| ? {($_.Count -gt 1) -and ($_.Group[0].ProductName -ne $Null)} `
| select -first 1 `
| fl
Name : Infragistics4.Shared.v13.1.dll, Infragistics4.Shared, 13.1.20131.2060
Count : 2
Group : {@{ProductVersion=13.1.20131.2060; FileName=Infragistics4.Shared.v13.1.dll; ProductName=Infragistics4.Shared},
@{ProductVersion=13.1.20131.2060; FileName=Infragistics4.Shared.v13.1.dll; ProductName=Infragistics4.Shared}}
Values : {Infragistics4.Shared.v13.1.dll, Infragistics4.Shared, 13.1.20131.2060}https://stackoverflow.com/questions/21143261
复制相似问题