Set-EntraUserLicense
將 Microsoft 線上服務的授權加入或移除使用者的授權清單。
語法
Default (預設值)
Set-EntraUserLicense
-UserId <String>
-AssignedLicenses <AssignedLicenses>
[<CommonParameters>]
Description
它Set-EntraUserLicense會將 Microsoft 線上服務的授權加入或移除到使用者的授權清單中。
對於委派情境,呼叫使用者至少需要以下一項 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
指定使用者的 ID(如 UserPrincipalName 或 ObjectId),在 Microsoft Entra ID 中。
參數屬性
| 類型: | 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。