Script Repository


Activate Office 365 service

October 18, 2018
1125

The script activates an 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.

Parameters:

  • $licenseSku - specifies the SKU Part Number of the license that the service belongs to;
  • $serviceToActivate - specifies the SKU Part Number of the office 365 service to activate;
  • $waitTime - specifies the time the script will wait for response from Office 365. If a response is not received within the time specified, the operation will be completed asynchronously.
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 \ Office 365.
  3. Double-click the 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
PowerShell
$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 Office 365 Object ID
try
{
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
    $objectId = $objectId.ToString()
}
catch
{
    $Context.LogMessage("The member doesn't have an Office 365 account", "Warning")
    return
}

# Get Office 365 Tenant credentials
$office365Cred = $Context.GetOffice365Credential()

# Check operation type

$scriptBlock = {
    param ($objectId, $licenseSku, $serviceToActivate, $office365Cred)
    Import-Module MsOnline

    function SearchLicense($licenses, $skuPartNumber)
    {
        foreach ($license in $licenses)
        {
            if (!($license.AccountSkuId.EndsWith($skuPartNumber)))
            {
                continue
            }
            return $license
        }
        
        return $NULL
    }
    
    # Connect to Office 365
    Connect-MsolService -Credential $office365Cred
    
    # 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 Office 365 E3 license."
        return
    }

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

        $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, $office365Cred
Wait-Job -Job $job -Timeout $waitTime

if ($job.State -ne "Completed")
{
    return
}

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


Comments ( 0 )
No results found.
Leave a comment