Introduction
A common task for migrations is to assign licenses to uses so that they can use Microsoft 365 services including Exchange Online. For some migrations, like Lotus Notes to Exchange Online we need to provision a users mailbox so that the third-party tools out there can push mailbox data to the new location. In either case, we can use the below code to assign a license to a group that assigns a license to the user and then with another code chunk we can verify that the mailbox is now present.
The Code
# Add user to group - Microsoft 365 License Function Add365License($CSV) { CLS Write-Host '' Write-Host 'Add mailboxes to Microsoft 365 License group.' -ForegroundColor Magenta Write-Host '' Foreach ($Line in $CSV) { $Found = $True Try { $User = Get-MsolUser -UserPrincipalName $Line.Email -ErrorAction STOP $UserID = ($User).ObjectId } Catch { $Found = $False } If ($Found) { Try { Add-MsolGroupMember -GroupObjectId <Group Object ID> -GroupMemberObjectId $UserID -ErrorAction STOP } Catch { $User = $Line.User Write-Host "$User --> " -NoNewLine Write-Host "Error message: $_.Exception.Message" -ForegroundColor Yellow } } Else { $Email = $Line.Email Write-Host "User at $Email not found." -ForegroundColor Yellow } } }
Once we have a license, we can then check to see if each mailbox is now present in our tenant.
Function MailboxCheck($CSV) { CLS Write-Host '' Write-Host 'Check for mailboxes' -ForegroundColor Magenta Write-Host '' Foreach ($Line in $CSV) { $User = $Line.Email Try { $MBX = Get-Mailbox $User -ErrorAction Stop Write-Host "Mailbox exists for " -ForegroundColor White -NoNewLine Write-Host "$User." -ForegroundColor Green } Catch { Write-Host "Mailbox not created for " -ForegroundColor White -NoNewLine Write-Host "$User" -ForegroundColor Yellow -NoNewLine Write-Host "... please check back later." -ForegroundColor White } } Write-Host '' }
Not that in order to execute this code, we need to pass a CSV file name to the functions like so:
Add365License $CSV MailboxCheck $CSV
Comments? Questions?
That’s it for this Quick PowerShell. Feel free to leave your Comments below!