0 votes

Hello,

Is there a PS script available to add a user to an o365 group (o365 only - not synced to AD) that can be used after user creation in a business rule?

Thanks very much.

by (540 points)

1 Answer

0 votes
by (213k points)
selected by
Best answer

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 }
}
0

Excellent, thank you.

0

A followup question please. Is there a PS script available that can remove a user from all groups in o365?
Thanks

0

Hello Jason,

Have a look at the following script in our repository: https://www.adaxes.com/script-repositor ... s-s360.htm.

0

Ah, I missed that script. Thanks again.

Related questions

0 votes
0 answers

Hi, I'm currently working with setting up a new Adaxes solution for a customer at my company. The customer needs a solution that can add a security group with full ... help me out here :-) That would be greatly appreciated. Best Regards, Kristoffer Høie.

asked Jun 29, 2016 by krihoie (20 points)
0 votes
1 answer

Hello, I would like to create a custom command "Add domain user to local admin group". The powershell command to execute this is quite easy. Where I'm struggling is another ... exist. Is it possible to create such a task without a custom form? Many thanks.

asked Aug 29, 2017 by HorstR (460 points)
0 votes
1 answer

Hello Back when we first started using Adaxes you created a couple of great scripts which worked together really well, the first one copied one users group membership and put in ... an addition to what groups the second user is already a member of? Thank you.

asked Aug 4, 2015 by CBurn (700 points)
0 votes
1 answer

I have tried using this Powershell script but it gives me this error

asked Apr 1, 2021 by rujimg (40 points)
0 votes
1 answer

Hello all, I'm trying to send an email to an O365 private group after a user account has expired. I'm currently able to send to single users, but whenever I specify ... , and the adaxes log shows no errors. Any pointers would be greatly appreciated. Thank you!

asked Aug 12, 2021 by lw.fa (20 points)
2,634 questions
2,369 answers
6,291 comments
971,518 users