Forum Discussion

DamienFR68's avatar
DamienFR68
Copper Contributor
Oct 17, 2023

Attributing Microsoft 365 licenses to a batch of users

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!

  • 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

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    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

      • DamienFR68's avatar
        DamienFR68
        Copper Contributor

        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? 

Resources