如何使用 PowerShell 強制刪除 Microsoft 365 中的聯繫人
我們取消了本地 Active Directory 中的所有聯繫人同步。但是,Microsoft 365 中仍然有一個聯繫人可用。當我們想要將其從 Microsoft 365 門戶中刪除時,它不允許我們這樣做,並且會失敗並出現錯誤。在本文中,您將了解如何使用 PowerShell 強制刪除 Microsoft 365 中的聯繫人。
讓我們看看當我們想要刪除 Microsoft 365 管理中心中的聯繫人時會發生什麼以及出現什麼錯誤。
- 登錄到Microsoft 365 管理中心
- 擴張用戶 > 聯繫人
- 選擇接觸從列表中
- 刪除聯繫方式
- 點擊刪除
出現刪除聯繫人失敗錯誤。
對 Identity 的操作失敗,因為它超出了當前用戶的寫入範圍。無法對該對象執行“Remove-MailContact”操作,因為該對像是從您的本地組織同步的。應對本地組織中的對象執行此操作。
那麼,如果您不再擁有本地組織並且聯繫人是剩餘的怎麼辦?讓我們看看強制刪除聯繫人的下一步。
要使用 PowerShell 強制刪除 Microsoft 365 (Exchange Online) 中的聯繫人,請執行以下步驟:
步驟 1. 安裝 Microsoft Graph PowerShell
以管理員身份運行 Windows PowerShell 並安裝 Microsoft Graph PowerShell。
安裝 Microsoft Graph PowerShell 和測試版。這是因為您需要使用 beta cmdlet 來刪除聯繫人。
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force
重要的:在運行 cmdlet 或腳本之前,請務必更新到最新的 Microsoft Graph PowerShell 模塊版本,以防止出現錯誤和不正確的結果。
步驟 2. 連接到 Microsoft Graph PowerShell
連接到 Microsoft Graph PowerShell。
Connect-MgGraph -Scopes OrgContact.Read.All, Directory.ReadWrite.All, Directory.Read.All
輸入您的全局管理員憑據並接受 Microsoft Graph 權限請求。
步驟 3. 使用 PowerShell 獲取 Microsoft 365 中的所有聯繫人
運行獲取 MgContact用於獲取聯繫人的 PowerShell cmdlet。這將列出所有聯繫人。
Get-MgContact -All | Sort-Object DisplayName
出現輸出。
DisplayName Id Mail MailNickname
----------- -- ---- ------------
Contact1 f1e8a63b-6961-4548-b7ea-caca8c7e5d47 [email protected] contact1
現在您已經有了聯繫人 ID,您可以仔細檢查並運行以下命令來檢索聯繫人。
Get-MgContact -OrgContactId "f1e8a63b-6961-4548-b7ea-caca8c7e5d47"
讓我們轉到下一步並刪除 Microsoft 365 中的聯繫人。
推薦閱讀:強制更改 Microsoft 365 中所有用戶的密碼
步驟 4. 使用 PowerShell 強制刪除 Microsoft 365 中的聯繫人
運行以下命令以從 Microsoft 365 中永久刪除聯繫人。
筆記:您必須使用Remove-MgBetaContact cmdlet,因為它尚未移植到穩定版本。
Remove-MgBetaContact -OrgContactId "f1e8a63b-6961-4548-b7ea-caca8c7e5d47"
第 5 步:驗證您的工作
檢查觸點是否已移除。
Get-MgContact -OrgContactId "f1e8a63b-6961-4548-b7ea-caca8c7e5d47"
該聯繫人已成功刪除,並且不再顯示在 Microsoft 365 管理中心中。
就是這樣!
結論
您了解瞭如何使用 PowerShell 強制刪除 Microsoft 365 中的聯繫人。強制刪除聯繫人的唯一方法是使用 Microsoft Graph PowerShell cmdlet。 Exchange Online PowerShell cmdlet 無法做到這一點。請記住,您需要安裝 Microsoft Graph Beta 模塊才能使用 cmdlet 並刪除聯繫人。
您喜歡這篇文章嗎?您可能還喜歡阻止從共享郵箱登錄。不要忘記關注我們並分享這篇文章。
