Hello Jason,
Below is the script you need. It adds the target user to the Office 365 distribution group specified in the $groupName variable.
$groupName = "My Group" # TODO: modify me
# Get the user's unique identifier in Office 365
try
{
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
$Context.LogMessage("The user doesn't have an account in Office 365", "Warning")
return
}
try
{
# Connect to Exchange Online
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Context.GetOffice365Credential() -Authentication Basic -AllowRedirection
Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Add-DistributionGroupMember"
# Add user to group
try
{
Add-DistributionGroupMember $groupName -Member $objectId.ToString() -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when adding the user to $groupName group. Error: " + $_.Exception.Message, "Warning")
}
}
finally
{
# Close the remote session and release resources
if ($session) { Remove-PSSession $session }
}