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 (294k 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, 2024 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, 2024 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

Looking for a script (unless there is a built in way, which I don't think there is) to grant User A full access to User B's OneDrive

asked Dec 16, 2024 by msheppard (660 points)
0 votes
1 answer

I'm currently writing an "After User Creation" rule and I have a PowerShell script that adds the newly created, on-premises synced user to a handful of cloud ... powershell cmdlet that fails. I need to use PowerShell for the additional condtional flexibility.

asked Dec 13, 2024 by smcfarland (60 points)
3,596 questions
3,283 answers
8,317 comments
548,255 users