PowerShell: Is an Object a Mailbox or Remote-Mailbox?

Before you start processing a bunch of Exchange objects sometimes you might want to check if they’re Mailboxes or RemoteMailboxes.  The script (and explanation) is after the break.The following script tests if an object is either a Mailbox or RemoteMailbox;  if you want to know WHICH it is I’ll write about how to customise it afterwards.  You can easily vary this to test for other sorts of object types too.

<# .SYNOPSIS Returns if an object is a valid mailbox or remotemailbox. .DESCRIPTION Returns if an object is a valid mailbox or remotemailbox. .PARAMETER ObjectToTest The object to test. #>  
function Get-IsValidMailObject
{
    param
    (
        [PsObject]$ObjectToTest=$Null
    )
    if ($ObjectToTest -ne $Null)
    {
        Write-Verbose "Object Type: $($ObjectToTest.GetType().ToString())"
        if ($ObjectToTest.GetType().ToString() -eq "Microsoft.Exchange.Data.Directory.Management.RemoteMailbox")
        {
            Write-Verbose "Remote mailbox object passed."
            Return $True
        }elseif ($ObjectToTest.GetType().ToString() -eq "Microsoft.Exchange.Data.Directory.Management.Mailbox")
        {
            Write-Verbose "Mailbox object passed."
            Return $True
        }else
        {
            Write-Error "Invalid object (not Mailbox or RemoteMailbox) passed."
            Return $False
        }
    }
    Return $False
}

The only obscure bits are the comparisons;

        if ($ObjectToTest.GetType().ToString() -eq "Microsoft.Exchange.Data.Directory.Management.RemoteMailbox")

This gets the type of an object and then converts it to a string name.  If that name matches the full type name of a RemoteMailbox then you know you’ve got the right object type!

The script returns a straight $True or $False about whether an object is either a Mailbox or RemoteMailbox.  If you want to return which, you could have it return strings instead;

function Get-WhichMailObject
{
    param
    (
        [PsObject]$ObjectToTest=$Null
    )
    if ($ObjectToTest -ne $Null)
    {
        Write-Verbose "Object Type: $($ObjectToTest.GetType().ToString())"
        if ($ObjectToTest.GetType().ToString() -eq "Microsoft.Exchange.Data.Directory.Management.RemoteMailbox")
        {
            Write-Verbose "Remote mailbox object passed."
            Return "RemoteMailbox"
        }elseif ($ObjectToTest.GetType().ToString() -eq "Microsoft.Exchange.Data.Directory.Management.Mailbox")
        {
            Write-Verbose "Mailbox object passed."
            Return "Mailbox"
        }else
        {
            Write-Error "Invalid object (not Mailbox or RemoteMailbox) passed."
            Return ""
        }
    }
    Return ""

}

Here if it returns an empty string (“”) you know the object is neither a Mailbox or RemoteMailbox.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: