Charly Kevin
Charly Kevin

Reputation: 3

O365, Manage Distribution List group with Powershell

Hope you're having a great day so far! I'm using O365 Online, and I'm trying to Add a user to a distribution list group with Powershell, to automate user creation. here are my steps

  1. Connect to MolService : Connect-MsolService

  2. I get the ObjectID of the distribution group.

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

  3. I get the user ObjectID

ObjectIDUser = Get-MsolUser -ObjectId $user.ObjectId

  1. I'm adding the user to the group

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

But here is the error:

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

Upvotes: 0

Views: 1725

Answers (1)

Avshalom
Avshalom

Reputation: 8889

As the error states: You cannot use the MSOL Cmdlets with Mail Enabled Objects, Use the Exchange Online Cmdlets for that:

Here's an helper function to load the Office 365 Exchange Cmdlets:

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
}

Use it like this:

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

Then use the relevant 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...

See this link

Upvotes: 1

Related Questions