我想使用 PowerShell 管理 Office 365,像是刪除 Office 365 的信箱,該如何處理?

  • 6672
  • 0
  • 2012-10-11

我想使用 PowerShell 管理 Office 365,例如使用 PowerShell 刪除 Office 365 的信箱,該如何操作才行?

 

2012/03/10 如果照此文執行 PowerShell 出現問題,請參考此文步驟後,再測試看看

使用 PowerShell 連接 PowerShell,出現錯誤【檔案無法載入,因為這個系統已停用指令碼執行】,該如何處理?

 

問題的來龍去脈

我想使用 PowerShell 管理 Office 365,例如使用 PowerShell 刪除 Office 365 的信箱,該如何操作才行?

 

問題的發生原因

想要使用 PowerShell 管理 Office 365,您必須先安裝一些軟體元件,安裝完成後,您就可以使用 PowerShell 連線到 Office 365 服務,對 Office 365 進行操作。

 

問題的解決方法

安裝必要條件

1. Windows PowerShell 和 .NET Framework 3.5.1。

2. Microsoft Online Services 登入小幫手。

Microsoft Online Services 登入小幫手 - 32 位元版

Microsoft Online Services 登入小幫手 - 64 位元版

下載後執行安裝檔進行安裝。

image

SNAGHTML4b2453

SNAGHTML4df24b

3. 若有需要使用 Cmdlet,請安裝適用於 Windows PowerShell 的 Microsoft Online Services 模組。

適用於 Windows PowerShell 的 Microsoft Online Services 模組 (32 位元版本)

適用於 Windows PowerShell 的 Microsoft Online Services 模組 (64 位元版本)

下載後執行安裝檔進行安裝

image

SNAGHTML4e7678

SNAGHTML4ef096

SNAGHTML4f3acf

SNAGHTML4f7530

SNAGHTML4ff94d

 

在本機電腦上將 Windows PowerShell 連線到 Office 365 雲端架構服務

1. 開啟 PowerShell。

image

2. 輸入指令


$LiveCred = Get-Credential

此時出現【Windows PowerShell 認證要求】視窗,輸入 Office 365 使用者名稱與密碼後,按【確定】。

image

2. 輸入指令


$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

AllowRedirection 參數可讓全世界資料中心的雲端架構組織使用相同的 URL,並將 Windows PowerShell 連線到 Office 365 雲端架構服務。

image

3. 輸入指令


Import-PSSession $Session

將用於 Office 365 雲端架構服務的命令匯入至本機電腦的用戶端工作階段,完成後您就可以開始在 PowerShell 執行命令操作信箱。

image

 

檢視與刪除在 Office 365 信箱

1. 使用【Get-Mailbox】可以檢視有關 Office 365 雲端架構信箱的資訊。

image

在 Office 365 您也可以檢視相同的信箱資訊。

image

2. 使用【Remove-Mailbox】指令可以刪除信箱。

image

在 Office 365,您也可以選擇信箱,然後按【刪除】按鈕進行刪除信箱的操作。

image

3. 使用【Get-RemovedMailbox】指令可以檢視可復原的已刪除郵箱。

image

在 Office 365,您可以點選【已刪除的信箱】按鈕,取得相同的資訊。

image

 

中斷 Windows PowerShell 與雲端架構服務的連線

1. 當您完成操作後,請記得中斷 PowerShell 的連線,輸入指令


Remove-PSSession <session variable>

image

2. 如果您沒有中斷伺服器端工作階段的連線就關閉 Windows PowerShell 視窗,您的連線將維持開啟狀態達 15 分鐘,您的帳戶一次只能具有三個伺服器端工作階段的連線。

 

其他相關資訊

使用 Windows PowerShell 管理 Office 365

將 Windows PowerShell 連線到服務

PowerShell Cmdlet 在 Exchange Online 中可用的參考