2017-02-22 50 views
0

我們必須遷移到Microsoft Azure,並且由於Azure無法處理Exchange中的AD組,因此我必須獲取spezifc組的成員,然後將其添加到交換中。所以我認爲我可以通過PowerShell實現這一點。將該AD組中的用戶替換爲該AD組中的用戶

的普林西普:

我的郵箱名,並且在這樣的CSV要提取的組:

Mailbox1 GroupForMailbox1

Mailbox2 GroupForMailbox2

Mailbox2 GroupForMailbox2

我知道如何從一個組中提取所有用戶:

Get-ADGroupMember -identity "GroupForMailbox1" -Recursive 

但問題是,在這個組中有一個組,我不想讓用戶離開它。讓我稱之爲「ExcludedGroup」。我如何獲得除羣組「ExcludedGroup」以外的所有廣告組成員?

然後,我必須把那些廣告的成員到特定的郵箱:

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

foreach ($Users){ 

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

,但我不適合這一切都在一個腳本,因爲知識的缺乏。

而我無法找到互聯網上的東西,雖然這是一個真正的問題與天藍。

我以爲有人可以幫助我。

回答

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 
    } 
} 
+0

完美的作品!感謝分享這個腳本:) –

相關問題