Function importExchangeModule{
$snapinLoaded = (get-pssnapin microsoft.exchange.management.* -ErrorAction SilentlyContinue).Name

$exchangeVersion=(GCM Exsetup.exe | % {$_.FileVersionInfo}).ProductVersion
$exchangeVersionMajor=$exchangeVersion.Substring(0,2);
$exchangeVersionMinor=$exchangeVersion.Substring(3,2);
$exchangeYear=0;
if (!($snapinLoaded)){

switch ([int]$exchangeVersionMajor){
6 {
$exchangeYear=2003;
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction Continue;
break;
}
8 {
$exchangeYear=2007;
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction Continue;
break;
}
14 {
$exchangeYear=2010;
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction Continue;
break;
}
15 {
$exchangeYear=2013;
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ErrorAction Continue;
}
15 {
if([int]$exchangeVersionMinor -eq 1){$exchangeYear=2016;}
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ErrorAction Continue;
}
15 {
if([int]$exchangeVersionMinor -eq 2){$exchangeYear=2019;}
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ErrorAction Continue;
}
}
"Exchange Server $exchangeYear is detected...`nThe appropriate Exchange PowerShell Snap-In has been imported...";
}
}

# This function is deprecated
function addAdminRoles{
$roles="Mailbox Import Export","Mailbox Search","Discovery Management"
# Exchange Admins need these three roles to perform operations on mailboxes: Organization Management, Mailbox Search, Mailbox Import Export, and Discovery Management
New-ManagementRoleAssignment -Name "Import Export_Organization Management" -SecurityGroup "Organization Management" -Roles $roles
}

#### Export all mailboxes to a network backup location ####
function exportAllMailboxes{
$storageUNC="\\FILESHERVER01\Backups\PSTs"
function monitorCompletion($check){
$migrationStatus=(Get-MailboxExportRequest -Mailbox $check).Status
$completed=$migrationStatus -like 'Completed'
if (!($completed)){
Write-Host -NoNewline "Waiting for $check batch to complete..."
$dots=50
$timeout=10 #10 seconds
while (!($completed)) {
$dots-=1;
#$timeout-=2; # Timeout is optional
#if($timeout -lt 0){"$timeout seconds have passed. Skip this waiting."; continue;}
if ($dots -eq 0){Write-Host ".";$dots=50;} # reload dots
else {Write-Host -NoNewline "."}
Start-Sleep -s 2
$completed.Refresh()
}
}
Write-Host "$check batch is completed."
}

$userAliases=Get-Mailbox -ResultSize unlimited
foreach ($userAlias in $userAliases){
New-MailboxExportRequest -Mailbox $userAlias -FilePath "$storageUNC\$userAlias.pst"
sleep 10;
# monitorCompletion $userAlias;
}
}

#### Create roles resport ####
function rolesReport{
$rolesReportFile="C:\rolesReport.csv"
$roles="Organization Management","Discovery Management"
$report=foreach ($role in $roles){
"$role`:`r-------------------------------------------------";
Get-RoleGroupMember $role | Format-Table;
"`n";
}
$report=$report|Out-String
$report+="`nOther Roles Assignments:`r-------------------------------------------------";
$assignments="Mailbox Import Export","Mailbox Search"
$report+=$assignments | %{Get-ManagementRoleAssignment -Role $_} | Select RoleAssigneename,Role,EffectiveUserName | Out-String
$report | Out-File -FilePath $rolesReportFile
}

#### Create mailbox resport ####
function mailboxesReport{
importExchangeModule;
$reportFile="C:\mailboxesReport.csv"
$sum=0;
$report=@()
$mailboxes=Get-Mailbox -ResultSize Unlimited | sort;
foreach ($mailbox in $mailboxes) {
$mailboxObject = (Get-MailboxStatistics $mailbox.SamAccountName)
$totalItems=$mailboxObject.TotalItemSize.Value.ToMB();
$totalDeleted=$mailboxObject.TotalDeletedItemSize.Value.ToMB();
$thisMailboxSize=$totalItems+$totalDeleted;
$report+=New-Object PsObject -property @{
'DisplayName'=$($mailbox.DisplayName)
'SamAccountName'=$($mailbox.SamAccountName)
'PrimarySmtpAddress'=$($mailbox.PrimarySmtpAddress)
'ServerName'=$($mailbox.ServerName)
'LastLogonTime'=$($mailboxObject.LastLogonTime)
'Database'=$($mailbox.Database)
'IsMailboxEnabled'=$($mailbox.IsMailboxEnabled)
'thisMailboxSize'=[int]$thisMailboxSize;
}
$sum+=$thisMailboxSize;
}
#$sumGB=$sum/1024;
$report+=New-Object PsObject -property @{
'DisplayName'="Total Storage For All Mailboxes"
'SamAccountName'=''
'PrimarySmtpAddress'=''
'ServerName'=''
'LastLogonTime'=''
'Database'=''
'IsMailboxEnabled'=''
'thisMailboxSize'=[int]$sum;
}
$report | Export-Csv -Path $reportFile -NoTypeInformation
"Reviewing exported contents...`n"
Import-Csv -Path $reportFile
}