Forum Discussion
Attributing Microsoft 365 licenses to a batch of users
- Oct 17, 2023
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
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?
- LainRobertsonOct 17, 2023Silver Contributor
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
- DamienFR68Oct 17, 2023Copper ContributorI have tried your modified version, but I still get the exact same error message.
- LainRobertsonOct 17, 2023Silver Contributor
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