对象数组与自定义标头的比较 [英] Object Array Comparison with Custom Headers
问题描述
我有一个从两个中心提取的角色对象数组.
I have an object array of roles I pulled from two vcenters.
我正在尝试制作一个有 3 列的格式化表:角色、主、从
I'm trying to make a formatted table that has 3 columns : roles, master, slave
后两列会有exists"或missing".
There would be "exists" or "missing" in the latter two columns.
我认为发生的问题是,当它发现该角色存在于 master 中时,它会以某种方式将 slave 列标记为缺失",即使我知道它在那里.
The problem I believe happening is when it finds that the role exists in the master, it somehow marks the slave column as "missing", even though I know it's there.
我才刚接触 Powershell 几个月,有人能看出我的逻辑有什么问题吗?
I'm only a few months into Powershell, can anyone see anything wrong with my logic?
RoleTable.AllRoles、RoleTable.MasterRoles 和 RoleTable.SlaveRoles 都是对象数组
edit: RoleTable.AllRoles, RoleTable.MasterRoles, and RoleTable.SlaveRoles are all object arrays
$RoleTable.AllRoles | Select-Object Name,
@{Name = "Master Server"; Expression = {if ($RoleTable.MasterRoles -contains $_) {"EXISTS"} else {"MISSING"}}},
@{Name = "Slave Server"; Expression = {if ($RoleTable.SlaveRoles -contains $_) {"EXISTS"} else {"MISSING"}}}
Roletable.Allroles 对象示例:
Example of Roletable.Allroles object:
$RoleTable.AllRoles[1] | Select-Object *
Description : Not logged-in user (cannot be granted)
IsSystem : True
PrivilegeList : {System.Anonymous}
ServerId : /VIServer=someuser@xxxx-xxxx:443/
Server :
Id : -4
Name : Anonymous
Uid : /VIServer=someuser@xxxx-xxxx:Role=-4/
ExtensionData : VMware.Vim.AuthorizationRole
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
推荐答案
除非 $RoleTable.AllRoles[1]
和 $RoleTable.SlaveRoles
中的项目参考 内存中完全相同的对象,-contains
将返回 $false
,即使 看似em> $RoleTable.SlaveRoles
中存在相同的对象.
Unless $RoleTable.AllRoles[1]
and an item in $RoleTable.SlaveRoles
refer to the exact same object in memory, -contains
will return $false
, even if a seemingly identical object exists in $RoleTable.SlaveRoles
.
(这仅适用于引用类型,而不适用于值类型)
(this is only true for reference types, not value types)
这应该说明区别:
对同一对象的引用:
PS C:\> $Collection = @(New-Object psobject -Property @{p=123})
PS C:\> $Item = $Collection[0]
PS C:\> $Collection -contains $Item
True
对相同对象的引用:
PS C:\> $Collection = @(New-Object psobject -Property @{p=123})
PS C:\> $Item = New-Object psobject -Property @{p=123}
PS C:\> $Collection -contains $Item
False
在您的示例中,您可以使用 Name
或角色 Id
属性(如@jisaak 所建议):
In your example, you could use the Name
or role Id
property (as suggested by @jisaak):
$RoleTable.AllRoles | Select-Object Name,
@{Name = "OnMaster"; Expression = {$RoleTable.MasterRoles.Id -contains $_.Id}},
@{Name = "OnSlave"; Expression = {$RoleTable.SlaveRoles.Id -contains $_.Id}}
如上所示,我可能会使用通用布尔值作为指标,而不是字符串
As shown above, I would probably use generic boolean values as indicators, rather than strings
这种行为是 .NET 中的一种刻意的性能优化 - 您永远不知道要确保对对象的所有内部属性进行精确的值比较,您需要深入到兔子洞的深处.
This behaviour is a deliberate performance optimization in .NET - you never know how deep down the rabbit hole you would need to go to ensure exact value comparison of all internal attributes of an object.
另一方面,值类型更容易执行相等比较(并且也不一定有要比较的内存引用),结果:
Value types, on the other hand, are easier to perform equality comparisons against (and also don't necessarily have a memory reference to compare), resulting in:
[int]
PS C:\> $Collection = @(283)
PS C:\> $Item = $Collection[0]
PS C:\> $Collection -contains $Item
True
PS C:\> $Item = 283
PS C:\> $Collection -contains $Item
True
[string]
:
PS C:\> $Collection = @("SomeString")
PS C:\> $Item = $Collection[0]
PS C:\> $Collection -contains $Item
True
PS C:\> $Item = "SomeString"
PS C:\> $Collection -contains $Item
True
等等...
这篇关于对象数组与自定义标头的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!