Script Repository

Activate Microsoft 365 service

June 23, 2020

The script activates a Microsoft 365 (Office 365) service that is a part of a license plan already assigned to a user. To activate a service with the help of the script, create a Custom Command that runs it for User objects.

For the script to work, you need to install Microsoft Azure Active Directory Module for Windows PowerShell on each computer where Adaxes service is running.


  • $serviceToActivate - Specifies the SKU Part Number of the Microsoft 365 (Office 365) service to activate.
  • $waitTime - Specifies the time the script will wait for response from Microsoft 365 (Office 365). If a response is not received within the time specified, the operation will be completed asynchronously.
  • $licenseSku - Specifies the SKU Part Number of the license that the service belongs to.

    How to get the SKU Part Number of a license plan in Adaxes:
    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 to which the license belongs.
    4. Click the necessary license plan. The SKU Part Number is displayed below the Display Name field.
Edit Remove
$licenseSku = "ENTERPRISEPACK" # TODO: modify me
$serviceToActivate = "EXCHANGE_S_ENTERPRISE" # Exchange Online Plan 2.  # TODO: modify me
$waitTime = 9 * 60 # TODO: modify me. Time in seconds

# Get Microsoft 365 object ID
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
    $objectId = $objectId.ToString()
    $Context.LogMessage("The member doesn't have a Microsoft 365 account", "Warning")

# Get Microsoft 365 tenant credentials
$microsoft365Cred = $Context.GetOffice365Credential()

# Check operation type
$scriptBlock = {
    param ($objectId, $licenseSku, $serviceToActivate, $microsoft365Cred)
    Import-Module MsOnline

    function SearchLicense($licenses, $skuPartNumber)
        foreach ($license in $licenses)
            if (!($license.AccountSkuId.EndsWith($skuPartNumber)))
            return $license
        return $NULL
    # Connect to Microsoft 365
    Connect-MsolService -Credential $microsoft365Cred
    # Get licenses assigned to user
    $user = Get-MsolUser -ObjectId $objectId

    # Find the necessary license
    $license = SearchLicense $user.Licenses $licenseSku
    if ($license -eq $NULL)
        Write-Error "Cannot activate the service because the user is not assigned the Microsoft 365 E3 license."

    # Enable the corresponding license plan
    $disabledPlans = @()
    foreach ($servicePlan in $license.ServiceStatus)
        if (($servicePlan.ProvisioningStatus -ne 'Disabled') -and ($servicePlan.ProvisioningStatus -ne 'None'))
        if ($servicePlan.ServicePlan.ServiceName -ieq $serviceToActivate)

        $disabledPlans += $servicePlan.ServicePlan.ServiceName

    # Save the changes
    $disabledServices = New-MsolLicenseOptions –AccountSkuId $license.AccountSkuId –DisabledPlans $disabledPlans
    Set-MsolUserLicense -ObjectId $objectId -LicenseOptions $disabledServices

# Start Windows PowerShell as a separate process and run the script in that process
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $objectId, $licenseSku, $serviceToActivate, $microsoft365Cred
Wait-Job -Job $job -Timeout $waitTime

if ($job.State -ne "Completed")

# Get output from external process
Receive-Job -Job $job

Comments ( 0 )
No results found.
Leave a comment