Assign Office 365 license with Powershell

I have trouble assigning licenses with the new AzureAD Powershell modules.
I know how to do it with the old MSOL cmdlets.
But somehow it never works with the new module.

I want to assign a Power BI Pro license.
If an E1 (STANDARDPACK) license is assigned, Power BI cannot be assigned.
Therefore I remove E1 and assign E3 + Power BI Pro

This is my script:

$ADDSKU="POWER_BI_ADDON","ENTERPRISEPACK"
$RemoveSKU = "STANDARDPACK"

# Create the objects we'll need to add and remove licenses
$AddLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$RemoveLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses

# Find the SkuID of the license we want to add and remove
$AddLicense.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $ADDSKU -IN).SkuID -join ","
$RemoveLicense.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $ADDSKU -IN).SkuID

# Set the Office license as the license we want to add in the $licenses object
$Licenses.AddLicenses = @()
$Licenses.RemoveLicenses = @()
$Licenses.AddLicenses = $AddLicense
$Licenses.RemoveLicenses =  $RemoveLicense

# Call the Set-AzureADUserLicense cmdlet to set the license.
Set-AzureADUserLicense -ObjectId "TestUser@onmicrosoft.com" -AssignedLicenses $licenses

Open in new window



Error I'm getting is:

Set-AzureADUserLicense : Error occurred while executing SetUserLicenses
Code: Request_BadRequest
Message: Cannot convert a primitive value to the expected type 'Edm.Guid'. See the inner exception for more details.

I've also tried to just asisgn one license to a user without any licenses and tried to remove a license from a user without adding any licenses. Nothing works. Always the same error.
LVL 3
alohadinSEAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
You need to provide a string value when removing licenses, not the full Microsoft.Open.AzureAD.Model.AssignedLicense object. Read more here: https://www.michev.info/Blog/Post/1705/removing-office-365-licenses-via-the-azuread-powershell-module
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alohadinSEAuthor Commented:
Thanks, that was indeed one of the issues.
Another issue is that I can't add multiple licenses and remove multiple licenses at once for some reason.
I get the same error if I specify multiple licenses in $AddLicense
1
Vasil Michev (MVP)Commented:
You can always do a loop and add each license individually.
0
alohadinSEAuthor Commented:
Indeed, that's what I did.

Here is the working script:

$AddSKU = "ENTERPRISEPACK","POWER_BI_ADDON"
$RemoveSKU = "STANDARDPACK"
$User = "TestUser@onmicrosoft.com"

# Create the objects we'll need to add and remove licenses
$AddLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$RemoveLicense = ""
$Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses

# Find the SkuID of the license we want to add and remove
[string[]]$AddLicenseSkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $AddSKU -IN).SkuID
[string[]]$RemoveLicenseSkuiD = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $RemoveSKU -IN).SkuID


# remove licenses
foreach ($SkuId in $RemoveLicenseSkuiD) {
    
    [string]$RemoveLicense = $SkuId
    $Licenses.AddLicenses = @()
    $Licenses.RemoveLicenses = @()
    $Licenses.RemoveLicenses = $RemoveLicense

    $user_licenses = (Get-AzureADUser -ObjectId "O365.AldinTest@deme-group.com").AssignedLicenses.SkuId
    $SkuPartNumber = Get-AzureADSubscribedSku | where {$_.SkuID -like $SkuId} | select -ExpandProperty SkuPartNumber
    
    if ($SkuId -in $user_licenses) {
        Set-AzureADUserLicense -ObjectId $User -AssignedLicenses $licenses
        Write-Host $SkuPartNumber "license has been removed!" -BackgroundColor Black -ForegroundColor Red
    } else {
        Write-Host $SkuPartNumber "was not assigned! - Skipping" -BackgroundColor Black -ForegroundColor Yellow
    }
}

# add licenses
foreach ($SkuId in $AddLicenseSkuId) {
    
    $AddLicense.SkuId = $SkuId
    $Licenses.AddLicenses = @()
    $Licenses.RemoveLicenses = @()
    $Licenses.AddLicenses = $AddLicense

    $user_licenses = (Get-AzureADUser -ObjectId $User).AssignedLicenses.SkuId
    $SkuPartNumber = Get-AzureADSubscribedSku | where {$_.SkuID -like $SkuId} | select -ExpandProperty SkuPartNumber
    
    if ($SkuId -notin $user_licenses) {
        Set-AzureADUserLicense -ObjectId $User -AssignedLicenses $licenses
        Write-Host "Assigned license:"$SkuPartNumber -BackgroundColor Black -ForegroundColor Green
    } else {
        Write-Host $SkuPartNumber "was already assigned! - Skipping" -BackgroundColor Black -ForegroundColor Yellow
    }
}

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.