AzureADLicense

Script – Automatically Assign Office 365 Licenses

 

This script connects to MS Online and assigns a license to any recently synchronised new accounts with the desired selected services enabled/disabled.

Before running this script connect to MS Online using the Azure Active Directory Shell. (Instructions Here)

# Details of the EA license Pack
$licPack = Get-MsolAccountSku | where {$_.AccountSkuId -eq "Tenant1:ENTERPRISEPACK"}
$licPack | ft -AutoSize | Out-Host
# Options when assigning (email only, all others excluded)
$licOptions = New-MsolLicenseOptions -AccountSkuId $licPack.AccountSkuId
$licOptions.DisabledServicePlans = @("BPOS_S_TODO_2", "FORMS_PLAN_E3", "STREAM_O365_E3", "Deskless", "FLOW_O365_P2", "POWERAPPS_O365_P2", "TEAMS1", "PROJECTWORKMANAGEMENT", "SWAY", "INTUNE_O365", "YAMMER_ENTERPRISE", "RMS_S_ENTERPRISE", "OFFICESUBSCRIPTION", "MCOSTANDARD", "SHAREPOINTWAC", "SHAREPOINTENTERPRISE")
# Check what licenses we have available
$licPackRemainingLicenses = $licPack.ActiveUnits - $licPack.ConsumedUnits
if ($licPackRemainingLicenses -lt 0) {$licPackRemainingLicenses = 0}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Check license assignments
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create an exclude list to filter out the tenancy service accounts etc
$excludeList = @()
$excludeList += "notifications@tenant1.com"
$excludeList += "zzADConnect@tenant1.com"
$excludeList += "admin@tenant1.onmicrosoft.com"
$users = @(Get-MsolUser -All | where {$_.UserPrincipalName -notin $excludeList} | sort DisplayName)
# Users without usage location
$nousage = @($users | where {$_.UsageLocation -eq $null})
if ($nousage.Count -eq 0) {
Write-Host "All users have correct location set" -ForegroundColor Green
}
else {
Write-Host "Setting location on the following $($nousage.Count) users.." -ForegroundColor Magenta
foreach ($u in $nousage) {
Write-Host "Setting location code on: $($u.DisplayName)" -ForegroundColor Cyan
Set-MsolUser -UserPrincipalName $u.UserPrincipalName -UsageLocation "GB"
}
Write-Host "Complete."
}
# unlicensed users
$unlicensed = @($users | where {$_.IsLicensed -eq $false})
if ($unlicensed.Count -eq 0) {
Write-Host
Write-Host "All users have licenses assigned" -ForegroundColor Green
} else {
Write-Host "There are $($unlicensed.Count) accounts that need licenses assigning to them." -ForegroundColor Magenta
if ($unlicensed.Count -le $licPackRemainingLicenses) {
Write-Host "There are sufficient licenses to proceed. Assigning licenses..." -ForegroundColor Magenta
foreach ($u in $unlicensed) {
Write-Host "Assigning license to: $($u.DisplayName)" -ForegroundColor Cyan
Set-MsolUserLicense -UserPrincipalName $u.UserPrincipalName -AddLicenses $licPack.AccountSkuId -LicenseOptions $licOptions
}
}
else {
Write-Host "The following $($unlicensed.Count) users are unlicensed but there are only $($licPackRemainingLicenses) licenses available. None updated." -ForegroundColor Red
$unlicensed | ft DisplayName | Out-Host
}
}

365 Guy

Microsoft Infrastructure Consultant @ Novosco. Specialising in Office 365, Azure AD & Exchange Server.

Leave a Reply

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

Check Also

Close
Close