AAD ScriptBox Item
The objective of this article is to introduce you to a process to assign licenses to your Office 365 users automatically. This process is based on a set of PowerShell scripts. You can find the required PowerShell script code at the end of this article.
The following table provides an overview of the related code files:
The description in this article assumes that these files exist in a folder called O365LicenseScripts on the hard drive of your computer.
The following diagram outlines all possible steps that part of a license assignment process:
To assign licenses, you need to provide the related source data that consists of the following components:
In the operational attribute, the SKU you intend to assign is expressed in form of the SKU name. For example, the value of this attribute is “DESKLESSPACK” if you want to assign this SKU to a user.
The SKU you want to assign and the service plans you want to disable are separated by using the pipe symbol (“|”). For example, if a user should be assigned to the E3 suite but not to SharePoint Online, the value of your operational attribute is:
ENTERPRISEPACK|SHAREPOINTENTERPRISE|SHAREPOINTWAC
Your source data is stored in a delimited text file that must have a specific header row, which is followed by the data rows.br> In this file, a semicolon is used as separator between the object identifier and the operational attribute. The following shows an example for the content of a source data file:
The process outlined in this article requires you to create a source data file. You can either create your source data file manually using the structure outlined in the previous section or you can use a script to retrieve data from your Active Directory Domain Service (ADDS).
If you want to use your on premise Active Directory Domain Service to create your source data file, you need to first populate the data about the SKU you want to assign and the service plans you want to disable. As soon as you have populated the required data, you can create your source data file by using a script.
In the section called “Understanding the Source Data Structure”, you have been introduced to the required data format for the attribute that is used to store the information about the SKU you want to assign and the service plans you want to disable. If you want to use your Active Directory Domain Service to store the related data, you need to populate an Active Directory attribute of the affected users with the required values.
The solution outlined in this article is based on a series of AD attributes that are used to locate affected objects and to store licence information. Depending on your approach to assign license information, you might be required to set certain attributes in Active Directory prior to running the related script.
If you choose to create the licensing input file in some other manner, you can skip the following step:
Option 1: User based license assignment example
This retrieves all users in the specified domain that have the userprincipalName attribute set and where the attribute ends with "@contoso.com". When the output file is created, the license information is added using the value from the extensionAttribute14 attribute. The following shows an example for a related output file:
Option 2: Group based license assignment
This retrives all users in the specified domain that are DIRECT members of the specified group (). When the output file is created, the license information is added using the value from the extensionAttribute14 attribute. The folowing shows an example for a related output file:
To create the source data file using a PowerShell script, you need to run the script called Get-LicensingInputFromAD.ps1. You can find the code for this script at the end of this article in the section called “PowerShell Script Code”. The script stores the created data file in a folder called queuedLicense.
If you plan on using Active Directory as your source, you can use the included SetupScript.ps1 script to configure the folders and scripts for use. You can re-run the Setup script at any point to change the configuration. If you want to run multiply copies of the script i.e. for group based assignment you can create multiple copies of the scripts in different folders and configure each script instance separately.
The process of assigning licenses to your Office 365 users consists of two steps:
Before you can run the AssignLicense.ps1 script, you need to configure the scripts to allow them to connect to your Microsoft Online Service tenant. To make the configuration you run the script called SetupScript.ps1. This script does also create the required folder structure and it turns the two template script files (AssignLicense.tmp, Get-MSOLUserLicensingReport.tmp) into actual PowerShell scripts.
You can find the code for this script at the end of this article in the section called “PowerShell Script Code”.
To connect you to your Microsoft Online Service tenant:
To assign the licenses, you need to run the AssignLicense.ps1 script. The script moves the processed source data file into the completedImportFiles folder.
In addition to this, it creates a logfile in the logs folder and logs events in the Application EventLog.
The objective of this section is to provide the script code you need to complete the process outlined in this article.