office 365通过PowerShell批量添加共享邮箱成员

问题描述 投票:0回答:1

我已经在o365中创建了共享邮箱。现在我需要将成员批量导入这些共享邮箱。

如何在powershell中做到这一点?我想做这样的事情

$users = import-csv -Path "C:\path\members.csv" -Delimiter ";"
Foreach ($user in $users){
    Add-mailboxpermission -identity "name of the shared mail box" -user $user -accessrights FullAccess
}

有什么想法吗 ?

powershell office365
1个回答
0
投票

连接到Office365将是一个很好的第一步:

$AdminUsername = "[email protected]" 
$AdminPassword = "YourPassword"
$AdminSecurePassword = ConvertTo-SecureString -String "$AdminPassword" -AsPlainText -Force
$AdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminUsername,$AdminSecurePassword

$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Admincredential -Authentication "Basic" -AllowRedirection
Import-PSSession $ExchangeSession

在你有一个会话后,你可以使用这些函数并添加一些逻辑:

$access = "FullAccess"
$mailbox = Get-Mailbox -Identity YourMailbox
$identity = $mailbox.UserPrincipalName
$permissions = Get-MailboxPermission -identity $identity

$users = Import-Csv -Path "C:\path\members.csv" -Delimiter ";" 
foreach($user in $users){
    try{
        $setPermissions = Add-MailboxPermission -Identity $identity -User $user -AccessRights $access
        Write-Host "Successfully added permissions for $user" -ForegroundColor Green
    }catch{
        Write-Host "Failed to add permissions for $user" -ForegroundColor Red
    }
}

请记住基于UserPrincipalName添加用户

© www.soinside.com 2019 - 2024. All rights reserved.