Introduction
Last week we covered connecting to the SharePoint PNP PowerShell module with Certificate Based Authentication. For this week we will break down a code that is used for migrating accounts from a resource Exchange on-premises forest an account forest. Why do this? Typically these activities occur during merger acquisitions or forest cleanup operations where the resource forest is being collapsed. In this article we will break down some useful code for this operation to better understand how it works.
Scenario Background and PowerShell Solution
Over the past few months I have been working with a client moving linked mailboxes to Exchange Online and part of the process includes an eventual collapse of the resource Exchange forest. A fellow Exchange MVP, Jaap Wesselius , wrote this great blog article on it and if this is your scenario, make sure to check out this link. The reason I wanted to lead with this is that he has some code snippets in the blog post that while a good Ctrl-C and Ctrl-V will allow you to take advantage of it, perhaps a bit of explanation of the code would be beneficial for you as it was for myself. So the point of this article is to take those code snippets, break them down and help you understand what it actually does.
The Code
$Mailboxes = Get-RemoteMailbox $Mailboxes | %{$p=$_.PrimarySmtpAddress; $r=$_.RemoteRoutingAddress; $g=$_.ExchangeGuid; $l=$_.LinkedMasterAccount; $a=$_.Alias; $d=$_.DisplayName; $h=$_.HiddenFromAddressListsEnabled; $L=(Get-User $a).LinkedMasterAccount ; $_.EmailAddresses | %{[psCustomObject] @{PrimarySmtpAddress=$p;RemoteRoutingAddress=$r; ExchangeGuid=$g; Alias=$a; LinkedMasterAccount=$l ;DisplayName=$d;HiddenFromAddressListEnabled=$h; EmailAddresses=$_}}} | export-Csv -path c:\mailbox\remote.csv -nti
That is one LONG line of code. Now let’s break this down!
There are a lot of ‘;’ characters which are used to represent new lines or where line returns could be used. Here is a visual guide to the parts of the script:
(1) Foreach-Object (orange)
First, the variable $Mailboxes which contains all properties of all Remote Mailboxes will be examined one at a time, which is noted by the ‘%’ symbol, an alias for the Foreach-Object PowerShell cmdlet.
(2) Mailbox Properties (yellow) / Variables (red)
In this step the properties we want to carry over are stored in variables. Not that each property looks like this in PowerShell: $_.
(3) Foreach-Object (second occurrence / red)
Once we have all of the properties we need stored in variables, we now take all email addresses of the remote mailbox ($_.EmailAddresses) and go through each one of the addresses.
(4) Custom PS Object Assembly
In this block, each property of the remote mailbox is combined with one email address and stored in a custom PS object. This process is repeated for each email address present for the remote mailbox.
(5) Export File Properties
In this section, the custom PS objects are exported via ‘Export-CSV’ to a file listed in the code (c:\Migration\RemoteMailboxes.csv from this code).
(6) -nti Switch
This switch is used to clean up the output of the CSV file so that some lines that are normally added are not. By doing so, the exported file will contain only headers and data and not extra characters.
There we have it. The code is complex and compact, but easy to use. Next week we will cover the exported CSV file and how to use it to populate user attributed in the account forest.
————————————————————————————————–
See previous Quick PowerShell Posts of the Week [ HERE ]
———————————————————————————————————–
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks: