'Set-AzureADUserLicense error when trying to create license object
I am trying to create a script for adding user licenses to O365. when I run the following line:
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
I get the following error:
Cannot find type [Microsoft.Open.AzureAD.Model.AssignedLicenses]: verify that the assembly containing this type is loaded.
I couldn't find anything to address this error and hoping for some help.
Solution 1:[1]
Based on my research that type is part of the AzureAD module, which is also pointed out here. What isn't pointed out is how to utilize the dll. Here's a little bit of code that should bring that namespace into your session and allow your command to work. It also deals with having more than one AzureAD module installed, which can be removed if you are certain you'll only ever have one.
Get-Module AzureAD -ListAvailable | Sort-Object Version | Select-Object -Last 1 | ForEach-Object {
$AzureADGraphDLL = $_.filelist -match '.*graph.*.dll$'
Import-Module $AzureADGraphDLL
}
Demo
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$license.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False AssignedLicenses System.Object
Solution 2:[2]
Import-Module -Name 'AzureAD' -UseWindowsPowershell
Import-Module -Name 'AzureAD' -NoClobber
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
This was tested on PowerShell v7. The current AzureAD module is not compatible with PSv7 and runs in a v5.1 remote session. This allows for the data types to be imported to the script session and run the cmdlets in the remote session.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Doug Maurer |
| Solution 2 | Aaron |
