SOLVED

Attributing Microsoft 365 licenses to a batch of users

Copper Contributor

Hello,

I am trying to add Microsoft 365 licenses to a batch of users using a csv file.

The script I use currently is the following:

 

$InputFile = "c:\temp\comptes.csv"
[array]$Users = Import-CSV $InputFile
ForEach ($User in $Users) { $License = Set-MgUserLicense -UserId $User.UPN -Addlicenses @{SkuId = '18181a46-0d4e-45cd-891e-60aabd171b4e'} -RemoveLicenses @()
}

 

 

But I obtain the following error:

 

Set-MgUserLicense : License assignment cannot be done for user with invalid usage location.
Status: 400 (BadRequest)
ErrorCode: Request_BadRequest
Date: 2023-10-17T08:55:55
Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 15d4ecb8-8b2f-40ca-9ae5-e7af94c112b9
client-request-id             : 378c66c6-20ce-49e7-bd62-8082d5b07b08
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"FR2PEPF000001DA"}}
x-ms-resource-unit            : 1
Cache-Control                 : no-cache
Date                          : Tue, 17 Oct 2023 08:55:55 GMT
Au caractère C:\Users\damien.hartmann\Documents\WindowsPowerShell\ajouter_licences_v3.ps1:3 : 29
+ ... n $Users) { $License = Set-MgUserLicense -UserId $User.UPN -Addlicens ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : ({ UserId = Olph...ionJsonSchema }:<>f__AnonymousType8`2) [Set-MgUserLicense_AssignExpanded], Exception
    + FullyQualifiedErrorId : Request_BadRequest,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded

 

Does anyone see the source of the error, and how to get around it?

Thanks!

9 Replies
best response confirmed by DamienFR68 (Copper Contributor)
Solution

@DamienFR68 

 

The error is fairly straight forward. Have a read of the following article:

 

 

This isn't an PowerShell issue, but rather than Azure licencing issue.

 

If you can't figure out which user is causing the error, try slightly changing your script to something like this instead:

 

$InputFile = "c:\temp\comptes.csv"
[array]$Users = Import-CSV $InputFile
ForEach ($User in $Users)
{
    try
    {
        $License = Set-MgUserLicense -UserId $User.UPN -Addlicenses @{SkuId = '18181a46-0d4e-45cd-891e-60aabd171b4e'} -RemoveLicenses @() -ErrorAction:Stop;
    }
    catch
    {
        throw "Script failed while attempting to set the licence for $($User.UPN). Aborting.";
    }
}

 

Cheers,

Lain

It's working now, thanks!

I am now trying to modify the script in order to remove the licenses of a list of users, modifying this line:

$License = Set-MgUserLicense -UserId $User.UPN -RemoveLicenses @{SkuId = '18181a46-0d4e-45cd-891e-60aabd171b4e'} -ErrorAction:Stop;


and I obtain a new error message :

Script failed while attempting to set the licence for email address removed for privacy reasons. Aborting.
Au caractère C:\Users\damien.hartmann\Documents\WindowsPowerShell\retirer_licences_office.ps1:11 : 9
+         throw "Script failed while attempting to set the licence for  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Script failed w...e.fr. Aborting.:String) [], RuntimeException
    + FullyQualifiedErrorId : Script failed while attempting to set the licence for email address removed for privacy reasons. Aborting.

 

Do you see what I should change? 

@DamienFR68 

 

Looking at the documentation, it looks like -RemoveLicenses is a string array, not a hashtable array:

 

 

So, perhaps try this slightly different version instead:

 

$License = Set-MgUserLicense -UserId $User.UPN -RemoveLicenses @('18181a46-0d4e-45cd-891e-60aabd171b4e') -ErrorAction:Stop;

 

Cheers,

Lain

I have tried your modified version, but I still get the exact same error message.

@DamienFR68 

 

Yeah, that makes sense if you're dropping that one line into the earlier script example I posted, since the try {} ... catch{} will be obscuring the error. That simple example was only intended to help you find the user with the usage location issue.

 

Here's a slightly different version of my earlier script that should give you the real error from the call to Set-MgUserLicense.

 

Example

$InputFile = "c:\temp\comptes.csv"
[array]$Users = Import-CSV $InputFile
ForEach ($User in $Users)
{
    try
    {
        $License = Set-MgUserLicense -UserId $User.UPN -RemoveLicenses @('18181a46-0d4e-45cd-891e-60aabd171b4e') -ErrorAction:Stop;
    }
    catch
    {
        Write-Warning -Message "Script failed while attempting to set the licence for $($User.UPN). Aborting.";
        throw;
    }
}

 

Just ensure you alter the CSV file to suit your new scenario - if you need to, that is.

 

Cheers,

Lain

Here is the new error message I obtain:

AVERTISSEMENT : Script failed while attempting to set the licence for email address removed for privacy reasons. Aborting.
Set-MgUserLicense : One or more parameters of the operation 'assignLicense' are missing from the request payload. The missing parameters are: addLicenses.
Status: 400 (BadRequest)
ErrorCode: Request_BadRequest
Date: 2023-10-17T13:43:32
Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 39115f47-9f38-4210-a969-17eb17b43509
client-request-id             : 515daba0-8bc5-4233-a83c-c1e8066a14ee
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"FR2PEPF000001DB"}}
x-ms-resource-unit            : 1
Cache-Control                 : no-cache
Date                          : Tue, 17 Oct 2023 13:43:32 GMT
Au caractère C:\Users\damien.hartmann\Documents\WindowsPowerShell\retirer_licences_office.ps1:7 : 9
+         $License = Set-MgUserLicense -UserId $User.UPN -RemoveLicense ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : ({ UserId = Olph...ionJsonSchema }:<>f__AnonymousType8`2) [Set-MgUserLicense_AssignExpanded], Exception
    + FullyQualifiedErrorId : Request_BadRequest,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded

Thank you for your support, much appreciated!

@DamienFR68 

 

Ah, okay. Again, a nice simple error. You just need to make sure you include the -AddLicenses parameter as shown in Example 6 from the documentation:

 

 

So, to once again slightly adjust my earlier script, it would now look like this:

 

Example

$InputFile = "c:\temp\comptes.csv"
[array]$Users = Import-CSV $InputFile
ForEach ($User in $Users)
{
    try
    {
        $License = Set-MgUserLicense -UserId $User.UPN -AddLicenses @() -RemoveLicenses @('18181a46-0d4e-45cd-891e-60aabd171b4e') -ErrorAction:Stop;
    }
    catch
    {
        Write-Warning -Message "Script failed while attempting to set the licence for $($User.UPN). Aborting.";
        throw;
    }
}

 

Cheers,

Lain

Thank you a lot, it works now!
Have a good day.