Set-EntraUserLicense
将Microsoft联机服务的许可证添加到用户分配的许可证列表中。
语法
Default (默认值)
Set-EntraUserLicense
-UserId <String>
-AssignedLicenses <AssignedLicenses>
[<CommonParameters>]
说明
将Set-EntraUserLicenseMicrosoft联机服务的许可证添加到用户分配的许可证列表中。
对于委派方案,呼叫用户至少需要以下Microsoft Entra角色之一。
- 目录编写人员
- 许可证管理员
- 用户管理员
注意:在分配许可证之前,请使用: 将使用情况位置分配给用户。 Set-EntraUser -ObjectId user@contoso.com -UsageLocation '<two-letter-country-code e.g. GB/US>'
示例
示例 1:基于模板用户向用户添加许可证
Connect-Entra -Scopes 'User.ReadWrite.All'
$licensedUser = Get-EntraUser -UserId 'TemplateUser@contoso.com'
$targetUser = Get-EntraUser -UserId 'SawyerM@contoso.com'
$sourceUserLicenses = $licensedUser.AssignedLicenses
$licensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
foreach ($license in $sourceUserLicenses) {
$assignedLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$assignedLicense.SkuId = $license.SkuId
$licensesToAssign.AddLicenses = $assignedLicense
Set-EntraUserLicense -UserId $targetUser.Id -AssignedLicenses $licensesToAssign
}
Name Value
---- -----
externalUserStateChangeDateTi…
businessPhones {8976546787}
postalCode 444601
createdDateTime 06-11-2023 04:48:19
surname KTETSs
jobTitle Manager
employeeType
otherMails {SawyerM@contoso.com}
isResourceAccount
usageLocation DE
legalAgeGroupClassification Adult
id cccccccc-2222-3333-4444-dddddddddddd
isLicenseReconciliationNeeded False
此示例演示如何基于模板用户向用户分配许可证。
-
-UserId参数指定用户的对象 ID(作为 UserPrincipalName 或 ObjectId)。 -
-AssignedLicenses参数指定要分配或删除的许可证列表。
示例 2:通过从其他用户复制许可证向用户添加许可证
Connect-Entra -Scopes 'User.ReadWrite.All'
$licensedUser = Get-EntraUser -UserId 'AdeleV@contoso.com'
$user = Get-EntraUser -UserId 'SawyerM@contoso.com'
$license1 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license1.SkuId = $licensedUser.AssignedLicenses.SkuId[0]
$license2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license2.SkuId = $licensedUser.AssignedLicenses.SkuId[1]
$addLicensesArray = @()
$addLicensesArray += $license1
$addLicensesArray += $license2
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $addLicensesArray
Set-EntraUserLicense -UserId $user.Id -AssignedLicenses $licenses
Name Value
---- -----
externalUserStateChangeDateTi…
businessPhones {8976546787}
postalCode 444601
createdDateTime 06-11-2023 04:48:19
surname KTETSs
jobTitle Manager
employeeType
otherMails {SawyerM@contoso.com}
isResourceAccount
usageLocation DE
legalAgeGroupClassification Adult
id cccccccc-2222-3333-4444-dddddddddddd
isLicenseReconciliationNeeded False
此示例演示如何通过从其他用户复制许可证来向用户分配许可证。
-
-UserId参数指定用户的对象 ID(作为 UserPrincipalName 或 ObjectId)。 -
-AssignedLicenses参数指定要分配或删除的许可证列表。
示例 3:删除分配的用户许可证
Connect-Entra -Scopes 'User.ReadWrite.All'
$userPrincipalName = 'SawyerM@Mcontoso.com'
$user = Get-EntraUser -UserId $userPrincipalName
$skuId = (Get-EntraUserLicenseDetail -UserId $userPrincipalName).SkuId
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.RemoveLicenses = $skuId
Set-EntraUserLicense -UserId $user.Id -AssignedLicenses $licenses
Name Value
---- -----
displayName SawyerM
id cccccccc-2222-3333-4444-dddddddddddd
jobTitle
surname M
mail
userPrincipalName SawyerM@contoso.com
mobilePhone
preferredLanguage
@odata.context https://graph.microsoft.com/v1.0/$metadata#users/$entity
businessPhones {}
officeLocation
givenName Sawyer
此示例演示如何通过检索用户详细信息删除用户的许可证。
-
-UserId参数指定用户的对象 ID(作为 UserPrincipalName 或 ObjectId)。 -
-AssignedLicenses参数指定要分配或删除的许可证列表。
示例 4:向多个用户批量分配许可证
Connect-Entra -Scopes 'Organization.ReadWrite.All'
# Retrieve the SkuId for the desired license plans
$skuId1 = (Get-EntraSubscribedSku | Where-Object { $_.SkuPartNumber -eq 'AAD_PREMIUM_P2' }).SkuId
$skuId2 = (Get-EntraSubscribedSku | Where-Object { $_.SkuPartNumber -eq 'EMS' }).SkuId
# Define the user to whom the licenses will be assigned
$users = ('AljosaH@Contoso.com', 'PalameeC@Contoso.com')
# You can, alternatively, import users from a csv file. For this example, the CSV should have a column named 'user'
$users = Import-Csv -Path "C:\path\to\your\users.csv" | Select-Object -ExpandProperty user
# Create license assignment objects
$license1 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license1.SkuId = $skuId1
$license2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license2.SkuId = $skuId2
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $license1, $license2
# Assign the licenses to each user
foreach ($user in $users$users) {
Set-EntraUserLicense -UserId $user -AssignedLicenses $licenses
}
参数
-AssignedLicenses
指定要分配或删除的许可证列表。
参数属性
| 类型: | AssignedLicenses |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | Named |
| 必需: | True |
| 来自管道的值: | True |
| 来自管道的值(按属性名称): | True |
| 来自剩余参数的值: | False |
-UserId
指定Microsoft Entra ID中的用户 ID(作为 UserPrincipalName 或 ObjectId)。
参数属性
| 类型: | System.String |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | 对象标识符 (ObjectId), UPN, Identity, UserPrincipalName |
参数集
(All)
| Position: | Named |
| 必需: | True |
| 来自管道的值: | True |
| 来自管道的值(按属性名称): | True |
| 来自剩余参数的值: | False |
CommonParameters
此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters。