Script Repository


Enable user in Office 365 after adding to group

March 23, 2016
935

The scripts enable a user in Office 365 after adding to a group. The 1st script assigns all licenses enabled in the user's 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 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 Office 365 properties
$office365Properties = $member.GetOffice365Properties()

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

# Set user location
$office365Properties.Location = $location

# Enable licenses
$licenses = $office365Properties.Licenses

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

# Save changes
$member.SetOffice365Properties($office365Properties)
$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 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 Office 365 license plans to enable
$planNames = $groupInfos["%distinguishedName%"]
if ($planNames -eq $NULL)
{
    return # No plans for this group
}

# Get Office 365 Properties
$office365Properties = $member.GetOffice365Properties()

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

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

# Save changes
$member.SetOffice365Properties($office365Properties)
$member.SetInfo()

Comments ( 0 )
No results found.
Leave a comment