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
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
- DamienFR68Oct 17, 2023Copper ContributorIt's working now, thanks!
- DamienFR68Oct 17, 2023Copper 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?
- 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