将Exchange中的AD组替换为该AD组中的用户

时间:2017-02-22 15:20:18

标签: powershell active-directory exchange-server

我们必须迁移到Microsoft Azure,因为Azure无法处理Exchange中的AD组,所以我必须获取spezifc组的成员,然后将它们添加到Exchange。所以我认为我可以用powershell来实现这个目标。

原则:

我有一个邮箱名称和必须在csv中提取的组,如下所示:

Mailbox1 GroupForMailbox1

Mailbox2 GroupForMailbox2

Mailbox2 GroupForMailbox2

我知道如何从群组中提取所有用户:

Get-ADGroupMember -identity "GroupForMailbox1" -Recursive

但问题是,该集团中有一个集团,我不想让用户离开它。让我称之为" ExcludedGroup"。我怎样才能获得除集团"除外集团"以外的所有AD集团成员?

然后我必须将这些AD成员放到特定的邮箱中:

$Users= "Users that I've got out of the upper command"

foreach ($Users){

Add-MailboxPermission -Identity "Mailbox1" -User $Users Accessright Fullaccess -InheritanceType all
}

但由于缺乏知识,我不能在一个脚本中适应这一切。

我无法在互联网上找到一些东西,尽管这是天蓝色的真正问题。

我以为有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

这样的东西?

#Sample data
$csv = @"
Mailbox,GroupName
Mailbox1,GroupForMailbox1
Mailbox2,GroupForMailbox2
Mailbox2,GroupForMailbox2
"@ | ConvertFrom-Csv

#Uncomment to import file
#$csv = Import-CSV -Path MyInputFile.csv

$ExcludedUsers = Get-ADGroupMember -Identity "ExcludedGroup" -Recursive | Select-Object -ExpandProperty SamAccountName

$csv | ForEach-Object {
    $mailbox = $_.Mailbox

    #Get members for group
    Get-ADGroupMember -Identity $_.GroupName -Recursive |
    #Remove unwanted users and keep only user objects (remove groups, computers etc.)
    Where-Object { ($ExcludedUsers -notcontains $_.SamAccountName) -and ($_.objectclass -eq 'user') } |
    ForEach-Object {
        #Grant each group member permission
        Add-MailboxPermission -Identity $mailbox -User $_.SamAccountName -AccessRights FullAccess -InheritanceType All
    }
}