联盟和交叉口PowerShell的? [英] Union and Intersection in Powershell?
问题描述
我有以下结构对象的数组:
I have an array of objects of the following structure:
structure Disk
{
int UID;
String Computer;
}
一个计算机可能有一大堆的共享磁盘,并且磁盘可能计算机之间共享。
我想找出所有常见的磁盘的所有计算机。
例如,我有计算机A,B和C;磁盘1,2,和3。
磁盘阵列是{1,A},{1,B},{2,A},{2,B},{2,C},{3,A}。
我想结果应该是磁盘2,因为它出现在A,B和C.
有没有办法实现这一目标的有效途径?
随着多个foreach循环是可以实现的,但肯定我想一个更好的办法。
我在考虑像交集操作,但在PowerShell中没有发现这一点。
A computer may have a bunch of shared disks, and a disk may be shared among computers. I want to find out all the disks common to all the computers. E.g., I have computer A, B, and C; Disks 1, 2, and 3. The disk array is {1,A}, {1,B}, {2,A},{2,B},{2,C},{3,A}. The result that I want should be the disk 2, because it appears on A, B, and C. Is there a effective way to achieve this? With multiple foreach loops it's achievable, but definitely I want a better way. I'm thinking about operations like intersection, but didn't find this in powershell.
推荐答案
假设 $改编
是数组,你可以这样做:
Assuming $arr
is the array, you can do like this:
$computers = $arr | select -expand computer -unique
$arr | group uid | ?{$_.count -eq $computers.count} | select name
在一般情况下,我将接近并和交在PowerShell中是这样的:
In general, I would approach union and intersection in Powershell like this:
$a = (1,2,3,4)
$b = (1,3,4,5)
$a + $b | select -uniq #union
$a | ?{$b -contains $_} #intersection
但对于你的要求,上述方案运作良好,并没有真正了解并和交在术语的标准定义。
But for what you are asking, the above solution works well and not really about union and intersection in the standard definition of the terms.
更新:
我已经写 pslinq 提供联盟列表
和相交名录
这有助于实现集并和交使用PowerShell。
I have written pslinq which provides Union-List
and Intersect-List
that help to achieve set union and intersection with Powershell.
这篇关于联盟和交叉口PowerShell的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!