License Assignment error

I have been running into error when assigning license to the user I created on my test environment. Here is the sample of the script. And please find the error message I get. The users are actually created on the tenant just that no license aassigned to them.

CODE

Connect-AzureAD
Connect-MgGraph

$userspath = "C:\Users\fuada\Downloads\LUsers2.csv"
$users  = Import-Csv $userspath
foreach ($user in $users){
$displayname = $user.DisplayName
$userprincipalname = $user.userprincipalname
$password = $user.password
$mailnickname = $user.MailNickname
$usagelocation = $user.Location

$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force


$passwordprofile = New-object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile 
$PasswordProfile.Password = $password
$passwordprofile.ForceChangePasswordNextLogin = $true



$newuser = New-azureaduser -DisplayName $displayname -UserPrincipalName $userprincipalname -UsageLocation $usagelocation -passwordprofile $passwordprofile -MailNickName $mailNickname -AccountEnabled $true

$licenseassignment = get-mgsubscribedsku -all | where skupartnumber -eq 'SPE_E3'
Set-mguserlicense -userID $userprincipalname -addlicenses @{SKUID = $licenseassignment.SkuId} -RemoveLicenses @()

}

error code:

Set-mguserlicense : Resource 'lola@faradaf.com' does not exist or one of its queried reference-property objects are not present.
Status: 404 (NotFound)
ErrorCode: Request_ResourceNotFound
Date: 2023-07-12T07:47:55
Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 408c35a6-6d8d-4ddd-9410-ebd579faae0e
client-request-id             : 9a32ea3b-85b8-4f3c-a3ec-6a740c03e2a2
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"UK South","Slice":"E","Ring":"3","ScaleUnit":"002","RoleInstance":"LO1PEPF000013F3"}}
x-ms-resource-unit            : 1
Cache-Control                 : no-cache
Date                          : Wed, 12 Jul 2023 07:47:54 GMT
At C:\Users\fuada\Downloads\licenseuser.ps1:25 char:1
+ Set-mguserlicense -userID $userprincipalname -addlicenses @{SKUID = $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({ UserId = lola...ionJsonSchema }:<>f__AnonymousType6`2) [Set-MgUserLicense_AssignExpanded], Exception
    + FullyQualifiedErrorId : Request_ResourceNotFound,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded
Set-mguserlicense : Resource 'tayo@faradaf.com' does not exist or one of its queried reference-property objects are not present.
Status: 404 (NotFound)
ErrorCode: Request_ResourceNotFound
Date: 2023-07-12T07:47:55
Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : e912fcef-7ad3-4f87-9df7-efe66f55b661
client-request-id             : 4656bb1e-0f02-4781-b6c3-cb81db48b6df
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"UK South","Slice":"E","Ring":"3","ScaleUnit":"002","RoleInstance":"LO1PEPF000013F3"}}
x-ms-resource-unit            : 1
Cache-Control                 : no-cache
Date                          : Wed, 12 Jul 2023 07:47:55 GMT
At C:\Users\fuada\Downloads\licenseuser.ps1:25 char:1
+ Set-mguserlicense -userID $userprincipalname -addlicenses @{SKUID = $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({ UserId = tayo...ionJsonSchema }:<>f__AnonymousType6`2) [Set-MgUserLicense_AssignExpanded], Exception
    + FullyQualifiedErrorId : Request_ResourceNotFound,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded

Could you please go back, edit your question once again and fix the formatting of your code and error messages?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Thanks, I have done that

Please can you help? Thank you

The only thing that jumps out at me is that you’re not specifying any scopes when calling
Connect-MgGraph - are these already set? If not, you’ll need to specify and accept them?

What is the output of the command below?

Get-MgContext | Select-Object -ExpandProperty Scopes

Edit: also, looking at the docs, I think it might be expecting a collection, even if the collection contains only one item:

$addLicenses = @(
    @{ SKUID = $licenseassignment.SkuId } 
)

Set-MgUserLicense -UserId $userprincipalname -AddLicenses $addLicenses -RemoveLicenses @()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.