Script Repository


Assign Office 365 license based on availability

July 03, 2017
1391

Using the script, you can assign Office 365 licenses based on their availability. For example, you can try assigning the Office 365 Enterprise E5 license, and if there are no available E5 licenses, assign an Office 365 Enterprise E3 one.

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. The script will specify a location for a user only when creating an Office 365 account. Locations of existing Office 365 accounts will not be modified.
  • $licenseSKUs - specifies the SKU Part Numbers of the license that should be assigned to the user. The script will attempt assigning the licenses exactly in the order you specify. The first license plan in the list that has at least 1 free license will be assigned to a user, and the rest of the list will be ignored.
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
$locationProperty = "c" # TODO: modify me
$licenseSKUs = @("ENTERPRISEPACK", "ENTERPRISEPREMIUM") # TODO: modify me

# Get associated tenant DN
$associatedTenantDN = $Context.TargetObject.AssociatedTenantDN
if ([System.String]::IsNullOrEmpty($associatedTenantDN))
{
    $Context.LogMessage("No associated tenant", "Warning")
    return
}

# Bind to tenant
$tenant = $Context.BindToObjectByDN($associatedTenantDN)

$skuToAssign = $NULL
foreach ($skuPartNumber in $licenseSKUs)
{
    # Get SKU
    $sku = $tenant.Skus | Where{$_.SkuPartNumber -eq $skuPartNumber}
    if ($sku -eq $NULL)
    {
        continue
    }
    
    # Check available licenses
    if ($sku.TotalUnits -eq $sku.ConsumedUnits)
    {
        continue
    }

    $skuToAssign = $skuPartNumber
    break
}

if ([System.String]::IsNullOrEmpty($skuToAssign))
{
    $Context.LogMessage("No licenses available", "Warning")
    return
}

# Get office365 Properties
$office365Properties = $Context.TargetObject.GetOffice365Properties()
if ([System.String]::IsNullOrEmpty($office365Properties.Location))
{
    # Get location from the specified property
    try
    {
        $location = $Context.TargetObject.Get($locationProperty)
    }
    catch
    {
        $Context.LogMessage("Location not specified. Office 365 account will not be activated", "Warning")
        return
    }
    
    # Set location
    $office365Properties.Location = $location
}

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

# Save changes
$Context.TargetObject.SetOffice365Properties($office365Properties)
$Context.TargetObject.SetInfo()


Comments ( 0 )
No results found.
Leave a comment