How to Use PowerShell to Automatically Assign Licenses to Your Office 365 Users

How to Use PowerShell to Automatically Assign Licenses to Your Office 365 Users

Windows Azure AD 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:

File

Description

SetupScript.ps1

Configures your environment for running the Licensing scripts. This script will prompt you for various parameters and will take use the three template files below to create customized scripts for your environment.

Setup of the scripted solution

In order to use this solution you should start by creating a subdirectory and place the four scripts below in a subdirectory of your choice. The SetupScript.ps1 will create the required subdirectory strucuture, prompt for O365 admin credentials and will
generate the Powershell scripts AssignLicense.ps1, Get-LicensingInputFromAD.ps1 and Get-MSOLUserLicensingReport.ps1.

It will also prompt your for a DefaultUsageLocation, which is required in Office 365 for a license assignment. The script will use the country configured of a user in your AD by default. If this attribute is not configured - the DefaultUsageLocation you
specify will be applied to the user. Please use the 2 letter country code from ISO-3166-1 alpha-2.

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.

Understanding the Source Data Structure

To assign licenses, you need to provide the related source data that consists of the following components:

An object identifier

An operational attribute that describes the SKU you want to assign and the service plans you want to disable

For group based licencense assignment, you need to provide an additional attribute on the group object to store the license information for the user account.

For user based license assignement, you need to provide the following attributes:

UserPrincipalName

SearchFilter (e.g.: "@contoso.com")

an attribute that stores the licence information

In the operational attribute, the SKU you intend to assign is expressed in form of the SKU name.

Information: Currently this solution does not handle the assignment of product combinations like Office 365 and CRM Online, Project Online and Visio Online. The command set-msoluserlicense needs to be called per product (SKU) and the Assignlicense.tmp
still needs to be modified between line 165 and 200 in order to handle this.

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

Note

Any non-licensing data in your employeeType attribute will result in no license set on that user, but it won’t replace any existing license assignments.

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.

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.

Preparing Your Active Directory Domain Service

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: