分配用户许可证时禁用对 Microsoft 365 服务的访问

此文章适用于 Microsoft 365 企业版和 Office 365 企业版。

Microsoft 365 订阅附带单个服务的服务计划。 Microsoft 365 管理员在向用户分配许可证时通常需要禁用某些计划。 按照本文中的说明,可以在使用 PowerShell 为单个用户帐户或多个用户帐户禁用特定服务计划的同时分配 Microsoft 365 许可证。

使用用于图表模块的 Azure Active Directory PowerShell

首先, 连接到 Microsoft 365 租户

接下来,使用此命令列出租户的许可证计划。

Get-AzureADSubscribedSku | Select SkuPartNumber

接下来,获取要向其添加许可证的帐户的登录名称,也称为用户主体名称 (UPN) 。

接下来,编译要启用的服务列表。 有关许可证计划的完整列表 (也称为产品名称) 、其包含的服务计划及其相应的友好名称,请参阅 许可的产品名称和服务计划标识符

对于下面的命令块,请填写用户帐户的用户主体名称、SKU 部件号以及用于启用和删除说明性文本和 < 和 > 字符的服务计划列表。 然后,在 PowerShell 命令提示符下运行生成的命令。

$userUPN="<user account UPN>"
$skuPart="<SKU part number>"
$serviceList=<double-quoted enclosed, comma-separated list of enabled services>
$user = Get-AzureADUser -ObjectID $userUPN
$skuID= (Get-AzureADSubscribedSku  | Where {$_.SkuPartNumber -eq $skuPart}).SkuID
$SkuFeaturesToEnable = @($serviceList)
$StandardLicense = Get-AzureADSubscribedSku | Where {$_.SkuId -eq $skuID}
$SkuFeaturesToDisable = $StandardLicense.ServicePlans | ForEach-Object { $_ | Where {$_.ServicePlanName -notin $SkuFeaturesToEnable }}
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = $StandardLicense.SkuId
$License.DisabledPlans = $SkuFeaturesToDisable.ServicePlanId
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $LicensesToAssign

使用用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块

首先, 连接到 Microsoft 365 租户

接下来,运行以下命令以查看当前订阅:

Get-MsolAccountSku

注意

PowerShell Core 不支持用于 Windows PowerShell 模块和 cmdlet 的其名称中包含 Msol 的 Microsoft Azure Active Directory 模块。 若要继续使用这些 cmdlet,必须从 Windows PowerShell 运行它们。

在命令的显示中 Get-MsolAccountSku

  • AccountSkuId 是组织名称>:<订阅>格式的组织<订阅。 <OrganizationName> 是你在 Microsoft 365 中注册时提供的值,对于你的组织是唯一的。 “订阅”<>值适用于特定订阅。 例如,对于 litwareinc:ENTERPRISEPACK,组织名称为 litwareinc,订阅名称为 ENTERPRISEPACK (Office 365 企业版 E3) 。

  • ActiveUnits 是已为订阅购买的许可证数。

  • WarningUnits 是订阅中尚未续订的许可证数,该许可证将在 30 天宽限期后过期。

  • ConsumedUnits 是已分配给订阅用户的许可证数。

请注意包含要许可的用户的 Microsoft 365 订阅的 AccountSkuId。 此外,请确保有足够的许可证来分配 (从 ActiveUnits) 中减去 ConsumedUnits

接下来,运行此命令以查看所有订阅中可用的 Microsoft 365 服务计划的详细信息:

Get-MsolAccountSku | Select -ExpandProperty ServiceStatus

从此命令的显示中,确定在向用户分配许可证时要禁用的服务计划。

下面是部分服务计划及其相应的 Microsoft 365 服务列表。

下表显示了 Microsoft 365 服务计划及其最常见服务的友好名称。 服务计划列表可能会有所不同。

服务计划 说明
SWAY
Sway
TEAMS1
Microsoft Teams
YAMMER_ENTERPRISE
Yammer
RMS_S_ENTERPRISE
Azure 权限管理 (RMS)
OFFICESUBSCRIPTION
以前名为 Office 365 专业增强版) 的Microsoft 365 企业应用版 (
MCOSTANDARD
Skype for Business Online
SHAREPOINTWAC
Office
SHAREPOINTENTERPRISE
SharePoint Online
EXCHANGE_S_ENTERPRISE
Exchange Online 计划 2

有关许可证计划的完整列表 (也称为产品名称) 、其包含的服务计划及其相应的友好名称,请参阅 许可的产品名称和服务计划标识符

现在,你已拥有要禁用的 AccountSkuId 和服务计划,你可以为单个用户或多个用户分配许可证。

对于单个用户

对于单个用户,请填写用户帐户的用户主体名称、AccountSkuId 和服务计划列表,以禁用和删除解释性文本和 < 和 > 字符。 然后,在 PowerShell 命令提示符下运行生成的命令。

$userUPN="<the user's account name in email format>"
$accountSkuId="<the AccountSkuId from the Get-MsolAccountSku command>"
$planList=@( <comma-separated, double-quote enclosed list of the service plans to disable> )
$licenseOptions=New-MsolLicenseOptions -AccountSkuId $accountSkuId -DisabledPlans $planList
Set-MsolUserLicense -UserPrincipalName $userUpn -AddLicenses $accountSkuId -ErrorAction SilentlyContinue
Sleep -Seconds 5
Set-MsolUserLicense -UserPrincipalName $userUpn -LicenseOptions $licenseOptions -ErrorAction SilentlyContinue

下面是名为 belindan@contoso.com的帐户的示例命令块,对于 contoso:ENTERPRISEPACK 许可证,要禁用的服务计划为 RMS_S_ENTERPRISE、SWAY、INTUNE_O365 和 YAMMER_ENTERPRISE:

$userUPN="belindan@contoso.com"
$accountSkuId="contoso:ENTERPRISEPACK"
$planList=@( "RMS_S_ENTERPRISE","SWAY","INTUNE_O365","YAMMER_ENTERPRISE" )
$licenseOptions=New-MsolLicenseOptions -AccountSkuId $accountSkuId -DisabledPlans $planList
Set-MsolUserLicense -UserPrincipalName $userUpn -AddLicenses $accountSkuId -ErrorAction SilentlyContinue
Sleep -Seconds 5
Set-MsolUserLicense -UserPrincipalName $userUpn -LicenseOptions $licenseOptions -ErrorAction SilentlyContinue

对于多个用户

若要为多个用户执行此管理任务,请 (包含 UserPrincipalName 和 UsageLocation 字段的 CSV) 文本文件创建逗号分隔值。 下面是一个示例:

UserPrincipalName,UsageLocation
ClaudeL@contoso.onmicrosoft.com,FR
LynneB@contoso.onmicrosoft.com,US
ShawnM@contoso.onmicrosoft.com,US

接下来,填写输入和输出 CSV 文件的位置、帐户 SKU ID 以及要禁用的服务计划列表,然后在 PowerShell 命令提示符下运行生成的命令。

$inFileName="<path and file name of the input CSV file that contains the users, example: C:\admin\Users2License.CSV>"
$outFileName="<path and file name of the output CSV file that records the results, example: C:\admin\Users2License-Done.CSV>"
$accountSkuId="<the AccountSkuId from the Get-MsolAccountSku command>"
$planList=@( <comma-separated, double-quote enclosed list of the plans to disable> )
$users=Import-Csv $inFileName
$licenseOptions=New-MsolLicenseOptions -AccountSkuId $accountSkuId -DisabledPlans $planList
ForEach ($user in $users)
{
$user.Userprincipalname
$upn=$user.UserPrincipalName
Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $accountSkuId -ErrorAction SilentlyContinue
sleep -Seconds 5
Set-MsolUserLicense -UserPrincipalName $upn -LicenseOptions $licenseOptions -ErrorAction SilentlyContinue
$users | Get-MsolUser | Select UserPrincipalName, Islicensed,Usagelocation | Export-Csv $outFileName
}

此 PowerShell 命令阻止:

  • 显示每个用户的用户主体名称。

  • 将自定义许可证分配给每个用户。

  • 创建包含已处理的所有用户的 CSV 文件,并显示其许可证状态。

另请参阅

使用 PowerShell 禁用对 Microsoft 365 服务的访问权限

使用 PowerShell 禁用对Sway的访问

使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组

使用 PowerShell 管理 Microsoft 365