我编写了这个脚本,它将群组中每个人的Exchange日历审阅者权限应用于该群组中的其他所有人。它有效,但有点慢。我想知道是否有人可以提出建议来提高循环效率?
$ADgroup = Get-ADGroupMember -Identity "My Group"
$ADgroup | foreach-object
{
$mb = Get-Mailbox -identity $_.SamAccountName
foreach ($person in $ADgroup)
{if ($person.objectGUID -ne $mb.Guid)
{
$name = $person.SamAccountName
Add-MailboxFolderPermission -Identity "${name}:\Calendar" -User $mb.Alias -AccessRights Reviewer
}
}
}
提前致谢
答案 0 :(得分:1)
如果您拥有Powershell Workflow的requirements,您可以使用它来并行运行任务(即内部foreach
)。还有jobs,但他们有点痛苦。
尽管如此,您可能需要将逻辑重构为:
workflow {
param($samsToAdd)
foreach -parallel($samToAdd in $samsToAdd) {
sequence{
$name = $samToAdd.name
$mb = $samToAdd.mb
Add-MailboxFolderPermission -Identity "${name}:\Calendar" -User $mb.Alias -AccessRights Reviewer
}
}
}
}
$ADgroup = Get-ADGroupMember -Identity "My Group"
$samsToAdd = @()
$ADgroup | foreach-object
{
$mb = Get-Mailbox -identity $_.SamAccountName
foreach ($person in $ADgroup)
{if ($person.objectGUID -ne $mb.Guid)
{
$name = $person.SamAccountName
$samsToAdd += new-object psobject -property @{
name = $name
mb = $mb
}
}
}
}
我还评论说,基于迭代性质,添加的权限在某种程度上是一项缓慢的任务。因此,并行操作只能获得如此多的收益,但IMO总是会有点慢。