Script Repository


Cancel user creation if there are no available Microsoft 365 licenses

June 23, 2020
1026

The script cancels creation of a new user if there are no available Microsoft 365 (Office 365) licenses in the specified plans of the tenant associated with the user. User creation is cancelled if the number of licenses that can be assigned to a new user is below a certain limit. To use the script in your environment, create a Business Rule triggering Before creating a user. For details, see Validate/Modify User Input Using a Script.

Parameter:

  • $limit - specifies the minimum number of licenses that must always be available. If the number of available licenses is below the limit, user creation is cancelled.
  • $skuPartNumbers - specifies the Microsoft 365 (Office 365) plans in which available licenses will be checked. If set to an empty array (i.e. @()), the number of available licenses will be checked in all Microsoft 365 (Office 365) plans.

    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
$limit = 2 # TODO: modify me
$skuPartNumbers = @("ENTERPRISEPACK", "OFFICESUBSCRIPTION") # TODO: modify me

if ([System.String]::IsNullOrEmpty("%adm-AssociatedO365Tenant%"))
{
    return # There are no Microsoft 365 Tenants associated with the new user
}
$tenant = $Context.BindToObjectByDN("%adm-AssociatedO365Tenant%")

# Check available licenses
$skuPartNumbers = New-Object "System.Collections.Generic.HashSet[System.String]" (,[string[]]$skuPartNumbers)
foreach ($sku in $tenant.Skus)
{
    if (($skuPartNumbers.Count -ne 0) -and 
        (-not($skuPartNumbers.Contains($sku.SkuPartNumber))))
    {
        continue
    }
    
    $difference = $sku.TotalUnits - $sku.ConsumedUnits
    if ($sku.Enabled -and ($difference -lt $limit))
    {
        # Cancel user creation
		$Context.Cancel("The number of Microsoft 365 licenses is insufficient. User will not be created.")
        return
    }
}


Comments ( 0 )
No results found.
Leave a comment