使用 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 @()

DisabledPlans 參數Set-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

開始使用適用於 Microsoft 365 的 PowerShell