We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Enable user in Microsoft 365 after adding to group

The scripts enable a user in Microsoft 365 (Office 365) after adding to a group. The 1st script assigns all licenses enabled in the user's Microsoft 365 (Office 365) Tenant, while the 2nd one enables only specific licenses depending on which group a user has been added to.

To use the scripts with Adaxes, create a Business Rule triggered after adding a member to a group that runs one of the scripts.

Assign All Licenses Enabled in a User's Tenant

Parameter:

  • $locationProperty - specifies a property of the user account that will be used as the user location in Microsoft 365 (Office 365). The value of the property must be represented by a two-letter country code per ISO 3166-1, for example, US or DE.
Edit Remove
PowerShell
$locationProperty = "c" # TODO: modify me

$member = $Context.BindToObjectEx("Adaxes://%member%", $True)
if ($member.Class -ine "user")
{
    return
}

# Get default Microsoft 365 properties
$microsoft365Properties = $member.GetMicrosoft365Properties()

# Get location from specified property
try
{
    $location = $member.Get($locationProperty)
}
catch
{
    $Context.LogMessage("Location not specified. Microsoft 365 account will not be activated", "Error")
    return
}

# Set user location
$microsoft365Properties.Location = $location

# Enable licenses
$licenses = $microsoft365Properties.Licenses

foreach ($license in $licenses)
{
    $license.Assigned = $True
}

# Save changes
$member.SetMicrosoft365Properties($microsoft365Properties)
$member.SetInfo()

Assign Specific Licenses Depending on the Group

Parameters:

  • $locationProperty - specifies a property of the user account that will be used as the user location in Microsoft 365 (Office 365). The value of the property must be represented by a two-letter country code per ISO 3166-1, for example, US or DE;
  • $groupInfos - specifies the Distinguished Names (DNs) of the groups and the corresponding license plans that should be assigned to users when added to each group. Each license plan must be represented as the corresponding SKU Part Number.
Edit Remove
PowerShell
$locationProperty = "c" # TODO: modify me
$groupInfos = @{
    "CN=My group 1,OU=Groups,DC=domain,DC=com" = @("ENTERPRISEPREMIUM", "SHAREPOINTSTORAGE")
    "CN=My group 2,OU=Groups,DC=domain,DC=com" = @("ENTERPRISEPACK")
} # TODO: modify me

$member = $Context.BindToObjectEx("Adaxes://%member%", $True)
if ($member.Class -ine "user")
{
    return
}

# Get the Microsoft 365 license plans to enable
$planNames = $groupInfos["%distinguishedName%"]
if ($planNames -eq $NULL)
{
    return # No plans for this group
}

# Get Microsoft 365 Properties
$microsoft365Properties = $member.GetMicrosoft365Properties()

# Check location
$microsoft365Location = $microsoft365Properties.Location
if ([System.String]::IsNullOrEmpty($microsoft365Location))
{
    # Get location from the specified property
    try
    {
        $location = $member.Get($locationProperty)
    }
    catch
    {
        $Context.LogMessage("Location not specified. Microsoft 365 account will not be activated", "Error")
        return
    }
    
    # Set location in Microsoft 365
    $microsoft365Properties.Location = $location
}

# Enable licenses
$licenses = $microsoft365Properties.Licenses
foreach ($license in $licenses)
{
    if ($planNames -contains $license.Sku.SkuPartNumber)
    {
        $license.Assigned = $True
    }
}

# Save changes
$member.SetMicrosoft365Properties($microsoft365Properties)
$member.SetInfo()
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers