Link to home
Start Free TrialLog in
Avatar of alohadin
alohadin

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alohadin
alohadin

ASKER

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
You can always do a loop and add each license individually.
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