Auto License Office 365 Migration Users Prior to Completing the Migration

Auto License Office 365 Migration Users Prior to Completing the Migration

When doing a migration to Office 365, one of the final steps prior to “flipping” the user in the migration batches, is to make sure to properly license them so once they flip they get an Exchange Online mailbox. One of the issues you will come across is you will have more users in Office 365 than you are migrating. This is very common because some users may not need Exchange services but may need other Office 365 offerings such as OneDrive, SharePoint, etc. This also happens when you use ADConnect to sync on-premise Active Directory users to Office 365 and again, not everyone will be needing an Exchange mailbox. However, prior to completing your migration batch jobs is all the users in the batches must have a proper license for Exchange.

Instead of going through user objects one by one, I created a script that will do the following:

  • Get all migration batches
  • Get all the users in all the migration batches
  • Find their Office 365 counter-part (because remember the migration user and the Office 365 are not 100% connected just yet)
  • Set their location code to the one you specify (Users with mailboxes MUST have a location code set)
  • Check to see if there is an available license to give from the license you specified users will get
  • If there is a license, it will license the user
  • Go onto the next user and repeat
  • Give end script stats that show how many users, how many got licensed, how many couldn’t get licensed and more

Pre-Requisites

First thing you will want to do before running the script, is to add enough licenses for your migration users. If you do not know the exact number of users you are migrating to Exchange Online try to shoot over your best guess and then take away any excess licenses at the end. The first line of the script is the license you are adding to your migration users. Remember, this is the AccountSkuID value. In my example I am licensing my users with an E1 license.

If you do not know the AccountSkuID of your license you can run Get-MsolAccountSku which will return something like this:

The next line (seen above) will want you to specify the Country Code or Usage Location. Microsoft follows the ISO country code list or the ISO “ALPHA-2” code which can be found here. In my example, all my users Usage Location was set to “US”

After you supply the license type and the country code you are ready to run the script. It will prompt your for your Office 365 credentials and then start parsing the migration batches. The Shell will display its progress and any warnings. In the picture below we can see the first user already had a valid license but the next user did not. If the second user’s batch completed it would error until you licensed the user.

The script will check to see if there is an available license prior to trying to assign it to a migration user by subtracting the ConsumedUnits from the ActiveUnits. The end of the script will display stats for that administrator’s may find helpful if they are dealing with a large amount of users.

Once all of the migration users are properly licensed, you can complete the migration batch and let the users “flip” and begin living off of Exchange Online. If you have any excess licenses in your tenant be sure to remove them so you are not billed for licenses that are not assigned.

Script

$License = "thelazyadministrator:STANDARDPACK"
$CountryCode = "US"


$NotFound = 0
$NotLic = 0
$AddLic = 0
$AlreadyLic = 0
$NotAbletoLic = 0

$UserCredential = Get-Credential -Message "Please enter your Office 365 credentials"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-MsolService -Credential $UserCredential

$Users = Get-MigrationUser | Select-Object -ExpandProperty Identity
Foreach ($User in $Users)
{
	Write-Host "Working on $User..." -ForegroundColor White
	$IdenClean = $User.Split("@")[0]
	Write-Host "Finding $User in Office 365..." -ForegroundColor White
	$Present = Get-MsolUser | Where-Object { $_.UserPrincipalName -like "*$IdenClean*" }
	If (!($Present))
	{
		Write-Warning -Message "$User not found!"
		$NotFound++
	}
	Else
	{
		Write-Host "$($Present.DisplayName) is in Office365!" -ForegroundColor Green
		Write-Host "Checking if $($Present.DisplayName) is licensed.." -ForegroundColor White
		$LicStatus = $Present | Select-Object -ExpandProperty isLicensed
		If ($LicStatus -eq $False)
		{
			$NotLic ++
			Write-Warning -Message "$($Present.DisplayName) is not licensed!"
			
			Write-Host "Checking to see if any there is an avaialble $License license to assign to $($Present.DisplayName)..." -ForegroundColor White
			$Consumed = Get-MsolAccountSku | Where-Object { $_.AccountSkuId -like "*$License*" } | Select-Object -ExpandProperty ConsumedUnits
			$Active = Get-MsolAccountSku | Where-Object { $_.AccountSkuId -like "*$License*" } | Select-Object -ExpandProperty ActiveUnits
			$AvailLic = $active - $Consumed
			If ($AvailLic -gt 0)
			{
				
				Write-Host "Setting $($Present.DisplayName)'s usage location to $CountryCode..." -ForegroundColor Yellow
				Set-MsolUser -UserPrincipalName ($Present).UserPrincipalName -UsageLocation $CountryCode
				Write-Host "licensing $($Present.DisplayName)..." -ForegroundColor Yellow
				Set-MsolUserLicense -UserPrincipalName ($Present).UserPrincipalName -AddLicenses $License
				$AddLic++
			}
			Else
			{
				$NotAbletoLic++
				Write-Warning -Message "Please purchase for $License licenses, there are $AvailLic left"
			}
		}
		Else
		{
			$AlreadyLic++
			Write-Host "$($Present.DisplayName) is licensed!" -ForegroundColor Green
		}
		
	}
}

Write-Host "
             END STATS
----------------------------------

Batch Users:$(($Users).count)
Users Not Found in Office 365 $NotFound
Users That Were Not Licensed: $NotLic
Users With Licenses Added: $AddLic
Users Not Able to be Licensed: $NotAbletoLic
Users Already Licensed: $AlreadyLic

-----------------------------------
" 

Get-PSSession | Remove-PSSession

 

 

One thought on “Auto License Office 365 Migration Users Prior to Completing the Migration

  1. Brad,

    As a junior sys admin based in Chicago myself, I’m frustrated that you’ve beat me to creating all the tools that I wanted to create for our own IT department. At the same time, I’m incredibly grateful for all of your hard work in the tools that you’ve made. If you’re ever looking to collaborate or run ideas by someone, please keep me in mind.

Leave a Reply

Your email address will not be published. Required fields are marked *