【发布时间】:2018-02-18 17:23:17
【问题描述】:
以下内容对我来说似乎很奇怪:
$user1 = Get-ADUser sameuser
$user2 = Get-ADUser sameuser
$user1 -eq $user2 # -> false
# the same for groups:
$group1 = Get-ADGroup samegroup
$group2 = Get-ADGroup samegroup
$group1 -eq $group2 # -> false
实际上,Powershell 用户似乎很高兴1 -eq 1 是真的。另外:
"1" -eq 1 # -> true
@("1") -contains 1 # -> true
但是:
$h1 = @{bla = 1}
$h2 = @{bla = 1}
$h1 -eq $h2 # -> false
$h1.GetHashCode(), $h2.GetHashCode() # -> 60847006, 5156994
# the above return values of course vary
$a1 = @(1;2;3)
$a2 = @(1;2;3)
$a1.GetHashCode(), $a2.GetHashCode() # -> 52954848, 34157931
# surprise, surprise:
$a1 -eq $a2 # no return value at all? (tested with versions 4.0 and 5.1)
($a1 -eq $a2).GetType() # or an Array?
($a1 -eq $a2).count # -> 0
除了这些有趣的行为之外,真正令人沮丧的是我不能简单地这样做:
$ones = Get-ADPrincipalGroupMembership one
$seconds = Get-ADPrincipalGroupMembership second
$excl_ones = $ones | ? { $_ -notin $seconds }
但必须这样做:
$second_nms = $seconds | % name
$excl_ones = $ones | ? { $_.name -notin $second_nms }
我错过了什么吗?
【问题讨论】:
-
您应该始终比较 AD 对象的
objectGUID值 -
您的
$a1 -eq $a2没有返回值,因为使用左侧数组的运算符充当对数组内容的过滤器。它不是询问数组 1 是否等于数组 2,而是询问 $a1 中等于 $a2 的所有项目。比较@(1,2,1,2) -eq 2 -
@TessellatingHeckler 这实际上是一个我不知道的好功能。但我们当然可以争辩说一致性看起来不同。
@(1,2,1,2) -eq 2 -> @(2,2)而2 -eq @(1,2,1,2) -> false。 -
@TNT 我会说设计对“所有小于 10 的值”更有意义:
@(1,4,12,16) -lt 10而不是“所有值为 2”,对于文本更是如此,所有行匹配一个正则表达式:@("line1", "line2", "line3") -match 'e[13]'那么你不会期望反向模式“十大于一个数字数组”或'e[13]' -match @("line1", "line2", "line3")有同样的意义。
标签: powershell active-directory