使用MsGraph分配许可证报错:无法将'EMSPREMIUM'转为Edm.Guid
解决Set-MgUserLicense分配许可证时的Edm.Guid转换错误
问题原因
Set-MgUserLicense的SkuId参数要求传入许可证的GUID标识,而非许可证的名称(如EMSPREMIUM)。你直接传入字符串EMSPREMIUM,导致系统无法将其转换为要求的Edm.Guid类型,从而抛出错误。
解决步骤
1. 获取目标许可证的SkuId(GUID)
通过Get-MgSubscribedSku查询已订阅的许可证列表,找到EMSPREMIUM对应的SkuId:
Get-MgSubscribedSku | Select-Object SkuId, SkuPartNumber
运行后会输出类似结果,复制EMSPREMIUM对应的SkuId值:
SkuId SkuPartNumber ------ ------------- xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx EMSPREMIUM yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy O365_BUSINESS_PREMIUM
2. 获取需要禁用的服务计划GUID(可选)
如果DisabledPlans使用MFA_PREMIUM名称也出现类似转换错误,需获取其对应的ServicePlanId:
# 替换为上述获取到的EMSPREMIUM的SkuId Get-MgSubscribedSku -SkuId "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | Select-Object -ExpandProperty ServicePlans | Select-Object ServicePlanId, ServicePlanName
找到MFA_PREMIUM对应的ServicePlanId并复制。
3. 修正后的许可证分配代码
将获取到的GUID填入变量,执行分配操作:
# 替换为EMSPREMIUM的SkuId GUID $license = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # 替换为MFA_PREMIUM的ServicePlanId GUID $DisabledPlans = @("zzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz") $AddLicense = @( @{ SkuId = $license DisabledPlans = $DisabledPlans } ) Set-MgUserLicense -UserId $upn -AddLicenses $AddLicense -RemoveLicenses @()
也可以用脚本自动获取GUID,避免手动复制:
# 自动获取EMSPREMIUM的SkuId $targetSku = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "EMSPREMIUM" } $license = $targetSku.SkuId # 自动获取MFA_PREMIUM的ServicePlanId $disabledPlan = $targetSku.ServicePlans | Where-Object { $_.ServicePlanName -eq "MFA_PREMIUM" } $DisabledPlans = @($disabledPlan.ServicePlanId) $AddLicense = @( @{ SkuId = $license DisabledPlans = $DisabledPlans } ) Set-MgUserLicense -UserId $upn -AddLicenses $AddLicense -RemoveLicenses @()
内容的提问来源于stack exchange,提问作者chocodeea
相关产品推荐
相关产品推荐

