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

Update Microsoft 365 services enabled for user to match tenant configuration

February 18, 2021 Views: 2575

The script updates Microsoft 365 services enabled for user to match tenant configuration. If a service is disabled in the tenant configuration and enabled for the user, the service will be disabled for the user. The script can be executed in a business rule, custom command or scheduled task configured for the User object type.

Parameter:

  • $licenseSku - Specifies the SKU part number of the Microsoft 365 license services in which should be updated. If you want to update services for all licenses, set the variable to $NULL.
    To get the SKU part number of a Microsoft 365 license:
    1. In Adaxes Administration Console, expand the service node that represents your Adaxes service.
    2. Navigate to Configuration\Cloud Services and select Microsoft 365.
    3. Double-click the Microsoft 365 (Office 365) tenant you need.
    4. Click the necessary license. The SKU part number is displayed below the Display Name field.
Edit Remove
PowerShell
$licenseSkuToCheck = "ENTERPRISEPACK" # TODO: modify me

# Get Microsoft 365 Object ID
try
{
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch  [System.Runtime.InteropServices.COMException]
{
    if ($_.Exception.ErrorCode -ne 0x8000500D)
    {
        throw $_.Exception
    }
    return # No Microsoft 365 account
}

# Get Microsoft 365 properties
$microsoft365Properties = $Context.TargetObject.GetOffice365Properties2()

# Get enabled Microsoft 365 licenses
$licenseInfos = @{}
foreach ($license in $microsoft365Properties.Licenses)
{
    if ($licenseSkuToCheck -ne $NULL)
    {
        if ($license.Sku.SkuPartNumber -ne $licenseSkuToCheck)
        {
            continue
        }
        
        if (!$license.Assigned)
        {
            return # The license is disabled for the user
        }
        
        $licenseInfos.Add($license.Sku.SkuPartNumber, $license)
        break
    }
    
    if (!$license.Assigned)
    {
        continue
    }
    
    # Assigned services
    $licenseInfos.Add($license.Sku.SkuPartNumber, $license)
}

if ($licenseInfos.Count -eq 0)
{
    return
}

# Bind to the Microsoft 365 tenant of the user
$tenantDN = $Context.TargetObject.Get("adm-AssociatedO365Tenant")
$tenant = $Context.BindToObjectByDN($tenantDN)

# Get services enabled in the tenant settings
foreach ($sku in $tenant.Skus)
{
    if (-not ($licenseInfos.ContainsKey($sku.SkuPartNumber)))
    {
        continue
    }
    $userLicense = $licenseInfos[$sku.SkuPartNumber]
    foreach ($tenantService in $sku.Services)
    {
        if ($tenantService.IsAutoAssignable)
        {
            continue
        }
        
        $userService = $userLicense.Services | Where {$_.Service.ServiceName -eq $tenantService.ServiceName}
        if ($tenantService.Enabled -eq $userService.Assigned)
        {
            continue
        }

        $userService.Assigned = $tenantService.Enabled
        $userLicense.AssignedModificationEnabled = $True
    }
}

if (!$microsoft365Properties.ContainsModifications)
{
    return
}

# Update Microsoft 365 properties of the user
$Context.TargetObject.SetOffice365Properties($microsoft365Properties)
$Context.TargetObject.SetInfo()

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers