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

Microsoft 365 licenses assigned to users

October 16, 2023 Views: 1464

The script generates a report containing users assigned the specified Microsoft 365 licenses. To execute the script, create a report with the corresponding custom column. The report should have a scope configured. In the report, Microsoft 365 licenses will be identified using their display names specified in the Microsoft 365 tenant settings.

Parameters:

  • $licensesColumnID - Specifies the identifier of the custom column that will store the assigned licenses. The column should be of the Text type. To get the identifier of a custom column:
    1. In the Report-specific columns section, on the Columns tab, right-click the custom column.
    2. In the context menu, navigate to Copy and click Column ID.
    3. The column identifier will be copied to clipboard.
  • $licensesParameterName - Specifies the name of the Checkbox list parameter used to select the licenses for report generation with the param- prefix. The values for selected items must be set to the SKU part numbers of the licenses.
  • $separator - Specifies the separator entered in the settings of the Checkbox list parameter for selecting licenses.
Edit Remove
PowerShell
$licensesColumnID = "{6e2ae115-7c27-4b52-a2d4-5e9d943edc92}" # TODO: modify me
$licensesParameterName = "param-Licenses" # TODO: modify me
$separator = ";" # TODO: modify me

# Get licenses to check
$licensesToCheck = $Context.GetParameterValue($licensesParameterName).Split($separator)

$criteria = New-AdmCriteria "user"
$Context.DirectorySearcher.AddCriteria($criteria)
try
{
    $searchIterator = $Context.DirectorySearcher.ExecuteSearch()
    while ($Context.MoveNext($searchIterator))
    {
        $searchResult = $searchIterator.Current
        $user = $Context.BindToObjectBySearchResult($searchResult)
        
        try
        {
            $office365Properties = $user.GetMicrosoft365Properties()
        }
        catch
        {
            continue
        }

        # Get Microsoft 365 licenses available for the user
        $licenses = $office365Properties.Licenses
        $licenseNames = New-Object System.Collections.ArrayList
        foreach ($license in $licenses)
        {
            if (!($license.Assigned))
            {
                continue
            }
            
            if ($licensesToCheck -notcontains $license.Sku.SkuPartNumber)
            {
                continue
            }
            
            $licenseNames.Add($license.Sku.DisplayName)
        }
        
        if ($licenseNames.Count -eq 0)
        {
            continue
        }
        
        # Add user to the report
        $Context.Items.Add($searchResult, @{ $licensesColumnID = ($licenseNames -join ";") }, $NULL)
    }
}
finally
{
    # Release resources
    if ($searchIterator) { $searchIterator.Dispose() }
}
Comments 4
avatar
Rick McMillin May 24, 2023
This script works great, but with a scope that contains a large number of users (a little over 1000), stops before completion and gives the following error:

The pipeline has been stopped.

Is there a limitation that can be adjusted to allow a larger scope?
avatar
Support May 25, 2023
Hello Rick,

The behavior is not related to Azure AD itself. The error is displayed because the time for the script execution exceeds the timeout set in Adaxes. By default, it is 10 minutes. It is recommended to just use scopes that allow the script to complete within the timeout. But if you want to increase it, please, specify the version of Adaxes you are currently using and we will get back to you with detailed instructions. For information on how to check your Adaxe version, see https://www.adaxes.com/help/CheckServiceVersion.
avatar
Rick McMillin May 25, 2023
Hello,

Thank you for the response and assistance. We are using version 3.15.20916.0.
avatar
Support May 26, 2023
Hello Rick,

Thank you for specifying. For details on how to manage configuration parameters, have a look at the following help article: https://www.adaxes.com/help/ChangeConfigurationParameters. You need to change the value of the Adsi.ScriptExecutionTimeout parameter.
Leave a comment
Loading...

Got questions?

Support Questions & Answers