Script Repository


Save assigned Microsoft 365 licenses to Active Directory attribute

May 08, 2020
1857

The script saves names of Microsoft 365 (Office 365) licenses assigned to a user to an attribute of the user's AD account. This can be used to quickly look up Microsoft 365 (Office 365) licenses assigned to multiple users. For example, you can add a column for the attribute to the Web Interface so that you are able to view assigned licenses when browsing Active Directory.

To use the script in your environment, create a Scheduled Task that runs it on a periodical basis. The task must be configured for the User object type.

Parameter:

  • $attributeLdapName - specifies the LDAP display name of the attribute that holds the license list.
Edit Remove
PowerShell
$attributeLdapName = "extensionAttribute1" # TODO: modify me

# Get Microsoft 365 properties
try
{
    $microsoft365Properties = $Context.TargetObject.GetOffice365Properties()
}
catch
{
    return # No Microsoft 365 Account
}

# Get Microsoft 365 licenses available for the user
$licenses = $microsoft365Properties.Licenses

# Build a list of assigned licenses, replacing SKU part numbers with display names
$licenseNames = New-Object "System.Text.StringBuilder"
foreach ($license in $licenses)
{
    if (!($license.Assigned))
    {
        continue
    }
    
    [void]$licenseNames.Append($license.Sku.DisplayName + ";")
}

if ($licenseNames.Length -eq 0)
{
    # No assigned licenses: clear the attribute
    $Context.TargetObject.Put($attributeLdapName, $NULL)
}
else
{
    # Save license names to the attribute
    $Context.TargetObject.Put($attributeLdapName, $licenseNames.ToString())
}

# Save changes
$Context.TargetObject.SetInfo()


Comments ( 0 )
No results found.
Leave a comment