Assign selective license in Office 365 using Powershell - "Unable to assign this license ..."

%3CLINGO-SUB%20id%3D%22lingo-sub-68815%22%20slang%3D%22en-US%22%3EAssign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20...%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68815%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20remove%20some%20services%20from%20users.%20%26nbsp%3BI%20have%20the%20following%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24ApprovedPlans%3D%22EXCHANGE_S_ENTERPRISE%22%2C%22SHAREPOINTENTERPRISE%22%2C%22SHAREPOINTWAC%22%2C%22MCOSTANDARD%22%2C%22OFFICESUBSCRIPTION%22%2C%22RMS_S_ENTERPRISE%22%2C%22YAMMER_ENTERPRISE%22%3CBR%20%2F%3E%24AccountSkuID%20%3D%20(Get-MsolAccountSku).AccountSkuID%3CBR%20%2F%3E%24AvailablePlans%20%3D%20(Get-MsolAccountSku).ServiceStatus.ServicePlan.ServiceName%3CBR%20%2F%3E%24DisabledPlans%20%3D%20%40()%3CBR%20%2F%3EForeach%20(%24Plan%20in%20%24AvailablePlans)%20%7Bif%20(%24ApprovedPlans%20-notcontains%20%24Plan)%20%7B%24DisabledPlans%2B%3D%24Plan%7D%7D%3CBR%20%2F%3E%24LicenseOptions%3DNew-MsolLicenseOptions%20-AccountSkuId%20%24AccountSkuID%5B0%5D%20-DisabledPlans%20%24DisabledPlans%3CBR%20%2F%3E%24Exceptions%3D%22name1%40domain.com%22%2C%22name2%40domain.com%22%3CBR%20%2F%3E%24AllUsers%3DGet-MSOLUser%20-all%20%7C%20where%20%7B%24_.IsLicensed%20-eq%20%22True%22%7D%20%7C%20sort%20DisplayName%3CBR%20%2F%3E%24Count%20%3D%200%3CBR%20%2F%3EForeach%20(%24User%20in%20%24AllUsers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%5BString%5D%24UPN%20%3D%20%24User.UserPrincipalName%3CBR%20%2F%3E%24Count%20%3D%20%24Count%20%2B%201%3CBR%20%2F%3E%24Processing%3D%22Processing%20%22%20%2B%20%24Count%20%2B%20%22%20of%20%22%20%2B%20%24AllUsers.Count%20%2B%20%22...%22%20%2B%20%24UPN%3CBR%20%2F%3EWrite-Host%20-NoNewline%20%24Processing.padright((%24host.ui.rawui.windowsize.width)-2)%22%60r%22%3CBR%20%2F%3Eif%20(Get-ADUser%20-f%20%7BUserPrincipalName%20-eq%20%24UPN%7D%20-SearchBase%20%22OU%3DUsers%2CDC%3DXXXXXX%2CDC%3DDOMAIN%2CDC%3DCOM%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(%24Exceptions%20-notcontains%20%24UPN)%20%7BSet-MsolUserLicense%20-UserPrincipalName%20%24UPN%20-LicenseOptions%20%24LicenseOptions%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3EWrite-Host%20%22%22%3CBR%20%2F%3EWrite-Host%20-NoNewline%20%22Disconnecting%20From%20MSOLService%22.PadRight(38%2C%22.%22)%3CBR%20%2F%3EGet-PSSession%20%7C%20Remove-PSSession%3CBR%20%2F%3EWrite-Host%20%22Done.%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tried%20to%20remove%20license%20(set-MSOLUserLicense%20-UserPrincipalName%20%24UPN%20-RemoveLicense%20%24AccountSKUID%5B0%5D%20(the%20license%20does%20get%20removed))%20and%20then%20add%20it%20using%20the%20above%20and%20I%20get%20the%20same%20error.%20What%20am%20I%20missing%20here%3F%20Thanks!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EZeff%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-68815%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ezeff_wheelock%40harvardpilgrim.org%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-69168%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-69168%22%20slang%3D%22en-US%22%3E%3CP%3EDOH!%20%26nbsp%3BNow%20that%20makes%20sense!%20%26nbsp%3BWe%20have%20two%20SKUs%2C%20PowerApps%20and%20ENTERPRISEPACK.%20%26nbsp%3BI%20made%20the%20following%20change%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24AvailablePlans%20%3D%20(Get-MsolAccountSku)%5B0%5D.ServiceStatus.ServicePlan.ServiceName%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I%20am%20getting%20the%20correct%20information!%20Thank%20you%20very%20much.%20%26nbsp%3BIt%20was%20driving%20me%20crazy.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-69079%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-69079%22%20slang%3D%22en-US%22%3E%3CP%3EAgain%2C%20how%20many%20SKUs%20do%20you%20have%20in%20your%20tenant%3F%20When%20you%20do%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24AccountSkuID%20%3D%20(Get-MsolAccountSku).AccountSkuID%0A%24AvailablePlans%20%3D%20(Get-MsolAccountSku).ServiceStatus.ServicePlan.ServiceName%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eyou%20get%20a%20combined%20set%20of%20SKUs%2FServicePlans%2C%20which%20you%20then%20use%20to%20set%20up%20the%20license%20options.%20This%20can%20end%20up%20with%20an%20incompatible%20combination.%20For%20example%2C%20in%20my%20tenant%20it%20results%20in%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPS%20C%3A%5C%26gt%3B%20%24LicenseOptions.AccountSkuId%0A%0AExtensionData%20AccountName%20SkuPartNumber%20%20%20%20%20%20%20%20%20%20%20%20%0A-------------%20-----------%20-------------%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20michev%20%20%20%20%20%20POWERAPPS_INDIVIDUAL_USER%0A%0A%0A%0APS%20C%3A%5C%26gt%3B%20%24LicenseOptions.DisabledServicePlans%0APOWERVIDEOSFREE%0APOWERFLOWSFREE%0APOWERAPPSFREE%0AEXCHANGE_S_FOUNDATION%0ABI_AZURE_P2%0ADeskless%0AFLOW_O365_P2%0APOWERAPPS_O365_P2%0ATEAMS1%0APROJECTWORKMANAGEMENT%0ASWAY%0AINTUNE_O365%0AEXCHANGE_S_FOUNDATION%0ABI_AZURE_P0%0ARMS_S_PREMIUM%0AINTUNE_A%0AAAD_PREMIUM%0AMFA_PREMIUM%3C%2FPRE%3E%3CP%3Ewhich%20is%20invalid.%20Either%20set%20it%20to%20the%20specific%20(enterprise%3F)%20SKU%20you%20want%20to%20configure%2C%20or%20do%20a%20proper%20cycle%20that%20covers%20each%20individual%20SKU%20and%20disables%20services%20accordingly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68951%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68951%22%20slang%3D%22en-US%22%3E%3CP%3EOK%2C%20I%20changed%20the%20%24AllUsers%20line%20to%20read%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24AllUsers%3DGet-MSOLUser%20-all%20%7C%20%3F%20%7B%24_.Licenses.AccountSkuId%20-eq%20%22tenant%3AENTERPRISEPACK%22%7D%20%7C%20sort%20DisplayName%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20this%20line%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24LicenseOptions%3DNew-MsolLicenseOptions%20-AccountSkuId%20%22tenant%3AENTERPRISEPACK%22%20-DisabledPlans%20%24DisabledPlans%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGot%20the%20same%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESet-MsolUserLicense%20%3A%20Unable%20to%20assign%20this%20license%20because%20the%20license%20options%20are%20invalid.%3CBR%20%2F%3EAt%20O%3A%5Coffice365%5CSetLicenses.ps1%3A57%20char%3A45%3CBR%20%2F%3E%2B%20...%20ains%20%24UPN)%20%7BSet-MsolUserLicense%20-UserPrincipalName%20%24UPN%20-LicenseOptio%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5BSet-MsolUserLicense%5D%2C%20MicrosoftOnlineException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Online.Administration.Automation.InvalidUserLicenseOptionException%2CMicrosoft.O%3CBR%20%2F%3Enline.Administration.Automation.SetUserLicense%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68939%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68939%22%20slang%3D%22en-US%22%3E%3CP%3ENo%2C%20simply%20make%20sure%20that%20the%20SKU%20you%20use%20to%20build%20the%20LicenseOptions%20variable%20is%20indeed%20assigned%20to%20the%20user.%20For%20example%2C%20you%20can%20adjust%20your%20filter%20to%20get%20only%20users%20with%20said%20SKU%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-MsolUser%20%7C%20%3F%20%7B%24_.Licenses.AccountSkuId%20-eq%20%22tenant%3AEMS%22%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eor%20whichever%20SKU%20corresponds%20to%20%24AccountSkuID%5B0%5D%26nbsp%3Bin%20your%20tenant.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68931%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68931%22%20slang%3D%22en-US%22%3E%3CP%3ESo%2C%20instead%20of%20having%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24LicenseOptions%3DNew-MsolLicenseOptions%20-AccountSkuId%20%24AccountSkuID%5B0%5D%20-DisabledPlans%20%24DisabledPlans%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20should%20put%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24LicenseOptions%3DNew-MsolLicenseOptions%20-DisabledPlans%20%24DisabledPlans%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EZeff%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68925%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20selective%20license%20in%20Office%20365%20using%20Powershell%20-%20%22Unable%20to%20assign%20this%20license%20..%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68925%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20should%20be%20covering%20two%20scenarios%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1)%20The%20user%20already%20has%20the%20same%20license%20applied%2C%20so%20only%20provide%20the%20LicenseOptions%3C%2FP%3E%3CP%3E2)%20The%20user%20does%20not%20have%20this%20license%20applied%2C%20so%20provide%20both%20the%20SKU%20and%20License%20options%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20of%20course%20the%20LicenseOptions%20should%20match%20the%20SKU%20set%2C%20which%20you%20have%20hardcoded%20to%20%24AccountSkuID%5B0%5D.%20Not%20every%20licensed%20user%20might%20have%20this%20same%20SKU%20assigned%2C%20make%20sure%20you%20check%20for%20that.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I am trying to remove some services from users.  I have the following script:

 

$ApprovedPlans="EXCHANGE_S_ENTERPRISE","SHAREPOINTENTERPRISE","SHAREPOINTWAC","MCOSTANDARD","OFFICESUBSCRIPTION","RMS_S_ENTERPRISE","YAMMER_ENTERPRISE"
$AccountSkuID = (Get-MsolAccountSku).AccountSkuID
$AvailablePlans = (Get-MsolAccountSku).ServiceStatus.ServicePlan.ServiceName
$DisabledPlans = @()
Foreach ($Plan in $AvailablePlans) {if ($ApprovedPlans -notcontains $Plan) {$DisabledPlans+=$Plan}}
$LicenseOptions=New-MsolLicenseOptions -AccountSkuId $AccountSkuID[0] -DisabledPlans $DisabledPlans
$Exceptions="name1@domain.com","name2@domain.com"
$AllUsers=Get-MSOLUser -all | where {$_.IsLicensed -eq "True"} | sort DisplayName
$Count = 0
Foreach ($User in $AllUsers)
{
[String]$UPN = $User.UserPrincipalName
$Count = $Count + 1
$Processing="Processing " + $Count + " of " + $AllUsers.Count + "..." + $UPN
Write-Host -NoNewline $Processing.padright(($host.ui.rawui.windowsize.width)-2)"`r"
if (Get-ADUser -f {UserPrincipalName -eq $UPN} -SearchBase "OU=Users,DC=XXXXXX,DC=DOMAIN,DC=COM")
{
if ($Exceptions -notcontains $UPN) {Set-MsolUserLicense -UserPrincipalName $UPN -LicenseOptions $LicenseOptions}
}
}
Write-Host ""
Write-Host -NoNewline "Disconnecting From MSOLService".PadRight(38,".")
Get-PSSession | Remove-PSSession
Write-Host "Done."

 

I have tried to remove license (set-MSOLUserLicense -UserPrincipalName $UPN -RemoveLicense $AccountSKUID[0] (the license does get removed)) and then add it using the above and I get the same error. What am I missing here? Thanks!

 

Zeff

6 Replies

You should be covering two scenarios:

 

1) The user already has the same license applied, so only provide the LicenseOptions

2) The user does not have this license applied, so provide both the SKU and License options

 

And of course the LicenseOptions should match the SKU set, which you have hardcoded to $AccountSkuID[0]. Not every licensed user might have this same SKU assigned, make sure you check for that.

So, instead of having:

 

$LicenseOptions=New-MsolLicenseOptions -AccountSkuId $AccountSkuID[0] -DisabledPlans $DisabledPlans

 

I should put:

 

$LicenseOptions=New-MsolLicenseOptions -DisabledPlans $DisabledPlans

 

?

 

Zeff

No, simply make sure that the SKU you use to build the LicenseOptions variable is indeed assigned to the user. For example, you can adjust your filter to get only users with said SKU:

 

Get-MsolUser | ? {$_.Licenses.AccountSkuId -eq "tenant:EMS"}

 

or whichever SKU corresponds to $AccountSkuID[0] in your tenant.

OK, I changed the $AllUsers line to read:

 

$AllUsers=Get-MSOLUser -all | ? {$_.Licenses.AccountSkuId -eq "tenant:ENTERPRISEPACK"} | sort DisplayName

 

and this line:

 

$LicenseOptions=New-MsolLicenseOptions -AccountSkuId "tenant:ENTERPRISEPACK" -DisabledPlans $DisabledPlans

 

Got the same error:

 

Set-MsolUserLicense : Unable to assign this license because the license options are invalid.
At O:\office365\SetLicenses.ps1:57 char:45
+ ... ains $UPN) {Set-MsolUserLicense -UserPrincipalName $UPN -LicenseOptio ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidUserLicenseOptionException,Microsoft.O
nline.Administration.Automation.SetUserLicense

Again, how many SKUs do you have in your tenant? When you do this:

 

$AccountSkuID = (Get-MsolAccountSku).AccountSkuID
$AvailablePlans = (Get-MsolAccountSku).ServiceStatus.ServicePlan.ServiceName

 

you get a combined set of SKUs/ServicePlans, which you then use to set up the license options. This can end up with an incompatible combination. For example, in my tenant it results in this:

 

PS C:\> $LicenseOptions.AccountSkuId

ExtensionData AccountName SkuPartNumber            
------------- ----------- -------------            
              michev      POWERAPPS_INDIVIDUAL_USER



PS C:\> $LicenseOptions.DisabledServicePlans
POWERVIDEOSFREE
POWERFLOWSFREE
POWERAPPSFREE
EXCHANGE_S_FOUNDATION
BI_AZURE_P2
Deskless
FLOW_O365_P2
POWERAPPS_O365_P2
TEAMS1
PROJECTWORKMANAGEMENT
SWAY
INTUNE_O365
EXCHANGE_S_FOUNDATION
BI_AZURE_P0
RMS_S_PREMIUM
INTUNE_A
AAD_PREMIUM
MFA_PREMIUM

which is invalid. Either set it to the specific (enterprise?) SKU you want to configure, or do a proper cycle that covers each individual SKU and disables services accordingly.

 

DOH!  Now that makes sense!  We have two SKUs, PowerApps and ENTERPRISEPACK.  I made the following change:

 

$AvailablePlans = (Get-MsolAccountSku)[0].ServiceStatus.ServicePlan.ServiceName

 

Now I am getting the correct information! Thank you very much.  It was driving me crazy.