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 (288k 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

I'm trying to automate adding users who are enrolled in MFA to an AD group. The scripts I found elsewhere here that do not work so I believe they may have been written against a prior Adaxes version or referencing a report that does not meet our needs.

asked May 31 by neal (50 points)
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

Hi all, How can I add a user directly to an Entra ID group? I understand it might be possible via CLI e.g. Add-AzureADGroupMember - But is there a built-in GUI method via Business Rules? Thanks, David

asked Oct 2 by dshortall (80 points)
0 votes
1 answer

I need a specific user, when requesting another user to join a group, to have an approval sent to the AD management team. I tried to create a "Business Rule", but I'm getting an "Access Denied" error. Any idea what this could be?

asked Aug 20 by fgmello (40 points)
3,541 questions
3,232 answers
8,225 comments
547,804 users