Exchange Tip: How to save your Exchange objects


If you are going to change mailbox properties by a script, you will need to be ready to roll back the change smoothly without delay. It means to have a recovery procedure.

Basically if you change any mailbox object (it can be applied to any PS object), you should save it before because of recovery or reporting purpose.

[PS] C:\> $mbx = Get-Mailbox "filip"

Well, we used variable $mbx to store object Get-Mailbox “filip”.

The variable contains fully-described object (properties, methods, values).

[PS] C:\> $mbx.EmailAddresses[0].Prefix.PrimaryPrefix 
SMTP

The variable and its data is defined by each Windows PowerShell provider. It means that it is alive based on provider memory and related session.

So how can we save it permanetly? We would export the object from PowerShell into the disk.

Object can be described by many ways but xml is most suitable format for representing our data structures and ensures also fully-described view. (Import-Clixml, Export-Clixml)

[PS] C:\> Get-Mailbox "filip" | Export-Clixml -Encoding unicode -path "D:\filip.xml"

The size of D:\filip.xml is about 0.6 MB (for 5 mailbox objects it is 0.96 MB), do not count 0.6 MB per mailbox.

[PS] C:\>(Get-Item D:\filip.xml).Length / 1MB
0.623746871948242

We have saved the mailbox and we can change for example its email addresses or anything else.

[PS] C:\> Set-Mailbox "filip" -EmailAddresses "SMTP:filip.kasaj@contoso.com"

After that we can revert the email addresses by D:\filip.xml anytime.

[PS] C:\> $f = Import-Clixml -Path "D:\filip.xml"
[PS] C:\> Get-Mailbox "filip" | Set-Mailbox -EmailAddresses $f.EmailAddresses

Well, feel free to use us the same procedure for any mass change regarding to server or recipient configuration and save your nerves during recovery.

Example:

$mailboxes = get-mailbox -ResultSize unlimited 
$mailboxes | Export-Clixml -Encoding unicode -path "D:\mailboxes.xml"
$mailboxes | Set-Mailbox -EmailAddressPolicyEnabled $True

##

$mailboxes = Import-Clixml -Path "D:\mailboxes.xml"
foreach($mailbox in $mailboxes){
    Set-Mailbox -Identity $mailbox.Identity -EmailAddresses $mailbox.EmailAddresses -EmailAddressPolicyEnabled $mailbox.EmailAddressPolicyEnabled
}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s