Hello,
The script can only be used in Adaxes 2021.1. Use the below script instead. For the script to work, install the Exchange EXO V2 module on the computer where Adaxes service runs.
Import-Module ExchangeOnlineManagement
$groupNames = @("MyGroup1", "MyGroup2", "MyGroup3")
try
{
# Get the object ID in Microsoft 365
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
return # The user doesn't have a Microsoft 365 account
}
Connect-ExchangeOnline -Credential $Context.GetOffice365Credential()
foreach ($groupName in $groupNames)
{
# Add user to group
try
{
Add-DistributionGroupMember $groupName -Member $objectId.ToString() -BypassSecurityGroupManagerCheck -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when adding the user to $groupName group. Error: " + $_.Exception.Message, "Warning")
}
}