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

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

从许可计划为 Microsoft 365 帐户分配许可证时,Microsoft 365 服务将从该许可证提供给用户。 但是,你可以控制用户可以访问的 Microsoft 365 服务。 例如,即使许可证允许访问 SharePoint Online 服务,也可以禁用对其进行访问。 可以使用 PowerShell 为特定许可计划禁用对任意数量的服务的访问权限,

  • 单个帐户。
  • 一组帐户。
  • 组织中的所有帐户。

注意

Microsoft 365 服务依赖项可能会阻止你在其他服务依赖指定的服务时禁用它。

使用 Microsoft Graph PowerShell SDK

注意

Azure Active Directory 模块将替换为 Microsoft Graph PowerShell SDK。 可以使用 Microsoft Graph PowerShell SDK 访问所有 Microsoft Graph API。 有关详细信息,请参阅 Microsoft Graph PowerShell SDK 入门

首先,使用 Microsoft Entra DC 管理员云应用程序管理员全局管理员帐户连接到 Microsoft 365 租户

为用户分配和删除许可证需要 User.ReadWrite.All 权限范围或“分配许可证”图形 API参考页中列出的其他权限之一。

读取租户中可用的许可证需要 Organization.Read.All 权限范围。

Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All

接下来,使用此命令查看可用的许可计划,也称为 SkuPartNumber:

Get-MgSubscribedSku | Select SkuId, SkuPartNumber, ServicePlans | Sort SkuPartNumber

有关详细信息,请参阅 使用 PowerShell 查看许可证和服务

若要查看本主题中过程前后的结果,请参阅 使用 PowerShell 查看帐户许可证和服务详细信息

为特定许可计划的特定用户禁用特定的 Microsoft 365 服务

若要为特定许可计划的用户禁用一组特定的 Microsoft 365 服务,请执行以下步骤:

首先使用以下命令列出租户中可用的许可计划。

Get-MgSubscribedSku | Select SkuPartNumber

SkuPartNumber
-------------
EMSPREMIUM
SPE_E5
RIGHTSMANAGEMENT_ADHOC

接下来,使用上述命令中的 SkuPartNumber,列出可用于给定许可证计划的服务计划 (Sku) 。

以下示例列出了可用于SPE_E5 (Microsoft 365 E5) 的所有服务计划。

Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5' |  select -ExpandProperty ServicePlans
AppliesTo ProvisioningStatus ServicePlanId                        ServicePlanName
--------- ------------------ -------------                        ---------------
User      Success            b21a6b06-1988-436e-a07b-51ec6d9f52ad PROJECT_O365_P3
User      Success            64bfac92-2b17-4482-b5e5-a0304429de3e MICROSOFTENDPOINTDLP
User      Success            199a5c09-e0ca-4e37-8f7c-b05d533e1ea2 MICROSOFTBOOKINGS
User      Success            6db1f1db-2b46-403f-be40-e39395f08dbb CUSTOMER_KEY
User      Success            4a51bca5-1eff-43f5-878c-177680f191af WHITEBOARD_PLAN3
User      Success            07699545-9485-468e-95b6-2fca3738be01 FLOW_O365_P3
User      Success            9c0dab89-a30c-4117-86e7-97bda240acd2 POWERAPPS_O365_P3
User      Success            e212cbc7-0961-4c40-9825-01117710dcb1 FORMS_PLAN_E5
User      Success            57ff2da0-773e-42df-b2af-ffb7a2317929 TEAMS1
User      Success            21b439ba-a0ca-424f-a6cc-52f954a5b111 WIN10_PRO_ENT_SUB
User      Success            eec0eb4f-6444-4f95-aba0-50c24d67f998 AAD_PREMIUM_P2
User      Success            c1ec4a95-1f05-45b3-a911-aa3fa01094f5 INTUNE_A
User      Success            7547a3fe-08ee-4ccb-b430-5077c5041653 YAMMER_ENTERPRISE
User      Success            a23b959c-7ce8-4e57-9140-b90eb88a9e97 SWAY
User      Success            e95bec33-7c88-4a70-8e19-b10bd9d0c014 SHAREPOINTWAC
User      Success            5dbe027f-2339-4123-9542-606e4d348a72 SHAREPOINTENTERPRISE
User      Success            b737dad2-2f6c-4c65-90e3-ca563267e8b9 PROJECTWORKMANAGEMENT
User      Success            43de0ff5-c92c-492b-9116-175376d08c38 OFFICESUBSCRIPTION
User      Success            0feaeb32-d00e-4d66-bd5a-43b5b83db82c MCOSTANDARD
User      Success            9f431833-0334-42de-a7dc-70aa40db46db LOCKBOX_ENTERPRISE
User      Success            efb87545-963c-4e0d-99df-69c6916d9eb0 EXCHANGE_S_ENTERPRISE

有关许可证计划的完整列表 (也称为产品名称) 、其包含的服务计划及其相应的友好名称,请参阅 许可的产品名称和服务计划标识符。 (使用 ServicePlanId 进行搜索,以查找服务计划的相应友好名称) 。

以下示例使用 MICROSOFTBOOKINGS (Microsoft Bookings) 分配SPE_E5 (Microsoft 365 E5) , (客户密码箱) 服务关闭LOCKBOX_ENTERPRISE:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$disabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("LOCKBOX_ENTERPRISE", "MICROSOFTBOOKINGS") | `
    Select -ExpandProperty ServicePlanId

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

DisabledPlansSet-MgUserLicense 参数的 -AddLicenses 属性将覆盖用户的现有DisabledPlans值。 若要保留现有服务计划的状态,必须将用户的当前服务计划状态与要禁用的新计划合并。

未能包含现有 DisabledPlans 项将导致启用用户以前禁用的计划。

以下示例使用SPE_E5 (Microsoft 365 E5) 更新用户,并关闭Sway和窗体服务计划,同时使用户的现有禁用计划保持其当前状态:

## Get the services that have already been disabled for the user.
$userLicense = Get-MgUserLicenseDetail -UserId "belinda@fdoau.onmicrosoft.com"
$userDisabledPlans = $userLicense.ServicePlans | `
    Where ProvisioningStatus -eq "Disabled" | `
    Select -ExpandProperty ServicePlanId

## Get the new service plans that are going to be disabled
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$newDisabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("SWAY", "FORMS_PLAN_E5") | `
    Select -ExpandProperty ServicePlanId

## Merge the new plans that are to be disabled with the user's current state of disabled plans
$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)
## Update user's license
Set-MgUserLicense -UserId "belinda@litwareinc.onmicrosoft.com" -AddLicenses $addLicenses -RemoveLicenses @()

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

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门