Retrieve Exchange Online Mailbox using EWS Managed API 2.2 and PowerShell


Retrieve Exchange Online Mailbox using EWS Managed API 2.2 and PowerShell


Summary

One of our clients asked for a PowerShell script which retrieves all the mailboxes Exchange Online is using EWS Managed API 2.2. Yes, like many we advised to use Exchange Online cmdlet Get-MailBox which has great parameters to drill down the data required. However, the client insisted for an alternate method and the reason is Get-Mailbox returns more data and if we use pipeline to select the required data (properties) the script takes more time or fails at times.

References

Solution

Refer to the method GetSearchableMailboxes and the example is below

$Service.GetSearchableMailboxes([string]::Empty,$false).SearchableMailboxes

Here is the ugly code which uses the clear text password and no inline comments. But, it works as expected.

Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
$Service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
$Service.Credentials = [System.Net.NetworkCredential]::new("chendrayan@contoso.onmicrosoft.com" , "SuperSecretPassword")
$Service.Url = "https://outlook.office365.com/EWS/Exchange.asmx"
$Service.GetSearchableMailboxes([string]::Empty,$false)

Output

Full Code

function Get-xMailBox
{
    <#
    .SYNOPSIS
        A PowerShell function to list all the searchable mailboxes using EWS Managed API 2.2
    .DESCRIPTION
        A PowerShell function to list all the searchable mailboxes using EWS Managed API 2.2.
        To know about EWS Managed API check references section
    .EXAMPLE
        PS C:\> Get-xMailBox
        List all SearchableMailboxes using default credentials
 
        PS C:\> Get-xMailBox -Credential 'admin@contoso.onmicrosoft.com'
        List all SearchableMailboxes using alternate credentials
 
        PS C:\> Get-xMailBox -Credential 'admin@contoso.onmicrosoft.com'  | Select SmtpAddress
        List only the SmtpAddress of all SearchableMailboxes
    .NOTES
        @ChendrayanV
        http://chen.about-powershell.com 
    #>
    [CmdletBinding()]
    param (
        [Parameter()]
        [System.Management.Automation.CredentialAttribute()]
        [pscredential]
        $Credential
    )
    
    try
    {
        Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
        $Service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
        if($PSBoundParameters.ContainsKey('Credential'))
        {
            $Service.Credentials = [System.Net.NetworkCredential]::new($Credential.UserName,$Credential.Password)
        }
        else 
        {
            $Service.UseDefaultCredentials = $true
        }
        $Service.Url = "https://outlook.office365.com/EWS/Exchange.asmx"
        $Service.GetSearchableMailboxes([string]::Empty,$false).SearchableMailboxes
    }
    catch
    {
        $_.Exception.Message
    }
}

Usage

help Get-xMailBox

help Get-xMailBox -Examples