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 (270k 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.

0

Hello,

A version of Adaxes the supports management of Azure AD objects is released. You can download it from here.

What’s New

Upgrade instructions

Related questions

0 votes
1 answer

Hello, I am wanting to write a script to have Adaxes add/remove all authorized DHCP Servers in the domain to a certain security group weekly. Dynamically adding and removing ... you have a better soulution then PS, then let me know. Thanks in advance!

asked Feb 8 by NewTechSolutions (20 points)
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

Hi team, I need to update users extensionAttribute6 after adding or removing them from a specific group. This is my setup: Group is updated based on rule set within Adaxes ... would like to update users after they were added or removed from this group. Thanks!

asked Sep 25, 2023 by wintec01 (1.1k 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)
3,326 questions
3,026 answers
7,727 comments
544,678 users