O365,使用 Powershell 管理分发列表组

O365, Manage Distribution List group with Powershell

祝您今天过得愉快! 我正在使用 O365 Online,我正在尝试使用 Powershell 将用户添加到通讯组列表组,以自动创建用户。 这是我的步骤

  1. 连接到 MolService:Connect-MsolService

  2. 我得到了通讯组的ObjectID

    $GroupeID = Get-MsolGroup -ObjectId $SupervisorGroup.ObjectId

  3. 我得到了用户ObjectID

ObjectIDUser = Get-MsolUser -ObjectId $user.ObjectId

  1. 我正在将用户添加到群组

Add-MsolGroupMember -GroupObjectId $GroupeID.ObjectId -GroupMemberObjectId $Object.ObjectId -GroupMemberType User

但错误是:

Add-MsolGroupMember : You cannot update mail-enabled groups using this cmdlet. Use Exchange Online to perform 
this operation.
At line:11 char:2
+  Add-MsolGroupMember -GroupObjectId $GroupeID.ObjectId -GroupMemberOb ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Add-MsolGroupMember], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MailEnabledGroupsNotSupportedException 
   ,Microsoft.Online.Administration.Automation.AddGroupMember

如错误所述:您不能将 MSOL Cmdlet 与启用邮件的对象一起使用,为此请使用 Exchange Online Cmdlet:

这是加载 Office 365 Exchange Cmdlet 的辅助函数:

Function Load-365ExchangeShell
{
Param(
[System.Management.Automation.PSCredential]
$Cred
)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -WarningAction SilentlyContinue -DisableNameChecking
}

这样使用:

$Cred = Get-Credential
Load-365ExchangeShell -Cred $Cred

然后使用相关的cmdlet (Add-DistributionGroupMember):

Add-DistributionGroupMember -Identity "DistributionGroupID_here" -Member "UserToAddID_here"

Note: for future use you better use the Updated Exchange Online V2 Module instead of the above method, as the old commands are deprecated...

看到这个link