Search code examples
powershellazure-powershellpowershell-1.0o365-flow

Choose between E3, E1 based on available licenses


I'm looking for a way to add an E3 or E1 license to a user based on the availability of the license.

For example: If there's no E3 license anymore, add then an E1 license.

What I do, I use the Set-MsolUserLicense command on 2 lines. If the first line gives an error like, no more available. Then it goes to the 2nd line and executes the second one.

If the first line goes without error and it goes to 2nd line and then it gives an error that the license is already added.


Solution

  • Based on the above shared requirement we have written the below powershell script will check the current units of licences available & assign the user with appropriate license accordingly either e3 or e1.

    Connect-MsolService
    
    $userlist={'[email protected]','[email protected]'}
    
    foreach($users in $userlist){
    $e3= Get-MsolAccountSku | Where AccountSkuId -Contains microsoft:ENTERPRISEPACK 
    $e1= Get-MsolAccountSku | Where AccountSkuId -Contains microsoft:STANDARDPACK
    
    if( $e3.ConsumedUnits -le $e3.ActiveUnits){
        Write-Host "Need to append e3"
        Set-MsolUserLicense -AddLicenses $e3.AccountSkuId  -UserPrincipalName $users
        }
        elseif( $e1.ConsumedUnits -le $e1.ActiveUnits){
    
        Write-Host "Need to append e1"
        Set-MsolUserLicense -AddLicenses $e3.AccountSkuId  -UserPrincipalName $users
        }
        else{
        Write-host "No active E1 or E3 Linceses available  for the current tenant"
        }
    
    }
    

    You can refer this documentation for more information about Product names and service plan identifiers for licensing