Need Help with a Script for Switching Licenses in Microsoft 365

0
5
Asked By NebulaDancer42 On

I'm in the process of transitioning from Office 365 E5 to Microsoft 365 E5 licensing and I'm trying to write a script to automate the license swap for everyone who currently has an E5 license. However, I'm facing an obscure error at one point in my script. I do understand that group-based licensing is an option, but due to some political reasons I won't get into, I'm not able to pursue that just yet. Currently, I'm testing my script using two test accounts before doing the rollout for everyone. Here's a snippet of my script:

```powershell
$e5Sku = Get-MgSubscribedSku -All | Where-Object {$_.SkuPartNumber -eq 'ENTERPRISEPREMIUM'}
$e5bettersku = Get-MgSubscribedSku -All | Where-Object {$_.SkuPartNumber -eq 'SPE_E5'}
$users = Get-MgUser -Filter "assignedLicenses/any(x:x/skuId eq $($e5sku.SkuId) )" -ConsistencyLevel eventual -CountVariable e5licensedUserCount -All | Where { ($_.UserPrincipalName -eq "[email protected]") -or ($_.UserPrincipalName -eq "[email protected]") }

foreach($user in $users) {
Set-MgUserLicense -UserID $user.Id -AddLicenses @{SkuId = $e5bettersku.SkuID} -RemoveLicenses @{SkuId = $e5sku.SkuID}
}
```

But I keep running into this error:

```
Line |
20 | Set-MgUserLicense -UserID $user.Id -AddLicenses @{SkuId = ($e5bet ...
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot convert the literal 'System.Collections.Hashtable' to the expected type 'Edm.Guid'. Status: 400 (BadRequest) ErrorCode: Request_BadRequest ...
```

I feel like I'm overlooking something simple, any ideas?

4 Answers

Answered By CodeWizard77 On

First off, try using the filter directly instead of grabbing everything and filtering later. Your current method may be too inefficient.

Also, break the script into clearer sections; it’s tough to read when it’s all smushed together. If the SKU UUID being returned is correct, you might want to double-check that. It's also worth noting you should probably use an array instead of a hashtable for your license removals – that could be part of your problem.

SmartCoder88 -

Good point! Just to add, using group licensing could save you a lot of hassle long-term. Even at smaller companies, manual user license management can become a real headache!

Answered By TechieTribe On

Your issue stems from how you're passing the SKU IDs. They expect a string or an array of strings, but you seem to be creating a hashtable with @{}. You should directly reference your SKU variable like this: `$e5sku.SkuId`. You can also use `Select -Expand SkuId` to get it directly if it’s a [guid] type. Also, be careful when transitioning licenses, especially with Teams phone systems – we had issues with number assignments when switching licenses too quickly!

SysAdminSquad -

And keep in mind, removing a license can disrupt dial-in capabilities for meetings if you're using Teams, so it’s good to avoid doing that all at once.

Answered By OfficeGuru On

Not totally sure why you're avoiding group-based licensing, but it’s a solid choice. It can definitely streamline your process, even if it requires some manual work on the group membership side. This could be a great opportunity to optimize your licensing strategies even further!

GlobalTechie -

I understand the hesitation. We also use PowerShell for groups because our users are spread internationally, and we manage different licenses based on location and contractor status. Just something to consider!

Answered By HelpfulHand On

I suggest trying out this adjusted command:

```powershell
Set-MgUserLicense -UserID $user.Id -AddLicenses @{SkuId = $e5bettersku.SkuID} -RemoveLicenses @{$e5sku.SkuID}
```

If that still doesn't work, check out this resource for a pre-built script that can help manage your licenses better. It’s user-friendly and supports various license management tasks: [O365 Reports License Management](https://o365reports.com/manage-365-licenses-using-ms-graph-powershell/). The process is pretty straightforward: generate a user report with E5 licenses, use it to apply the new Microsoft 365 E5 license, and then remove old licenses.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.