Assign Office 365 license with Powershell

alohadin
alohadin used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Distinguished Expert 2018
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

Author

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
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You can always do a loop and add each license individually.

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial