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