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

Assign Microsoft 365 license based on availability

December 29, 2022 Views: 4646

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

Parameters:

  • $locationProperty - specifies a property of the user account that will be used as the user location in Microsoft 365 (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 a Microsoft 365 (Office 365) account. Locations of existing Microsoft 365 (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 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
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 Microsoft 365 Properties
$microsoft365Properties = $Context.TargetObject.GetMicrosoft365Properties()
if ([System.String]::IsNullOrEmpty($microsoft365Properties.Location))
{
    # Get location from the specified property
    try
    {
        $location = $Context.TargetObject.Get($locationProperty)
    }
    catch
    {
        $Context.LogMessage("Location not specified. Microsoft 365 account will not be activated", "Warning")
        return
    }
    
    # Set location
    $microsoft365Properties.Location = $location
}

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

# Save changes
$Context.TargetObject.SetMicrosoft365Properties($microsoft365Properties)
$Context.TargetObject.SetInfo()

Comments 2
avatar
Andrew Baker May 22, 2024
Could this be adapted to assign another license if there are none available?

We are currently in a unique position where we have M365 E3/E5 seperated out into its separate skus and we now have had to purchase more of these licenses as we have more staff and they are combined.

So basically the question is, can we add a overflow license if no Office 365 E5 is available and then if no license is available assign an M365 E5 license?
avatar
Support May 22, 2024
Hello Andrew,

As it is mentioned in the script description, the $licenseSKUs variable specifies the SKU Part Numbers of the licenses 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 one free license will be assigned to a user, and the rest of the list will be ignored. As such, all you need to do is populate the $licenseSKUs variable with the SKUs you need in the corresponding order.
Leave a comment
Loading...

Got questions?

Support Questions & Answers