1
我只是想与我拥有的东西一起五连胜,这种情况有点过于复杂,无法解释标记。通过广告组成员身份进行高效迭代
XML配置:
<Groups>
<Group priority="1" name="testGroup1" profile="testProfile1"/>
<Group priority="2" name="testGroup2" profile="testProfile2"/>
<Group priority="3" name="testGroup3" profile="testProfile3"/>
<Group priority="4" name="testGroup4" profile="testProfile4"/>
</Groups>
PowerShell的:
$userProfileHash = [ordered]@{}
# Itterate through groups
foreach ($group in ($homeNode.ApplicationMethod | ? {$_.id -eq "adgroup"}).Groups.Group | Sort-Object $_.priority) {
# Get group members, Itterate through them
Get-ADGroupMember -Identity $group.name | ForEach-Object {
# Add to ordered hash
try {
$userProfileHash.Add($($_.samaccountname),$group.profile)
} catch {#don't want to see errors, there may be many}
}
}
现在,我将要解释一下:
为了防止用户成为其成员超过1个定义的组。我按优先级对组进行排序(在foreach表达式中),然后使用哈希表规则,即该键必须是唯一的。
这确保每个用户只添加一次,并且他们将被分配他们首先出现的组的配置文件。
我不太喜欢这种方法,但它是我尝试过的最快的方法:1100秒测试用户5秒。
问:
我如何可以加快这个代码? (这可能有成千上万的用户,当它在生产中)