#Load Exchange 2010 Module
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010 -ErrorAction SilentlyContinue
#Setup Variables
$Filename = "C:\temp\tasks\fowarding_address\export.csv"
$header = "UPN,ForwardingSmtpAddress,DeliverToMailboxAndForward"
$header | Out-File $Filename
#Grab Data
$Users = Get-Mailbox -ResultSize Unlimited -Filter { (ForwardingAddress -ne $null) -or (ForwardingSmtpAddress -ne $null) }
ForEach ($User in $Users)
{
if ($user.ForwardingSmtpAddress -ne $null)
{
$forwardingSmtpAddress = $user.ForwardingSmtpAddress
$DeliverToMailboxAndForward = $User.DeliverToMailboxAndForward.ToString()
$logstring = "$($user.PrimarySmtpAddress),$($forwardingSmtpAddress),$($DeliverToMailboxAndForward)"
$logstring | Out-File $Filename -Append
}
else
{
if ($user.ForwardingAddress -ne $null)
{
$forwardingContact = $user.ForwardingAddress
$forwardingContactAddress = (Get-Recipient $forwardingContact).PrimarySmtpAddress
$DeliverToMailboxAndForward = $User.DeliverToMailboxAndForward.ToString()
$logString = "$($user.PrimarySmtpAddress),$($forwardingContactAddress),$($DeliverToMailboxAndForward)"
$logString | Out-File $Filename -Append
}
}
}
#Remove Exchange 2010 Module
Remove-PSSnapin Microsoft.Exchange.Management.Powershell.E2010
#Connect to Exchange Online
$username = "<user name>"
$targetdir = (dir $env:LOCALAPPDATA”\Apps\2.0\” -Include CreateExoPSSession.ps1,Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse | Group Directory | ? {$_.Count -eq 2}).Values | sort LastWriteTime -Descending | select -First 1 | select -ExpandProperty FullName
import-Module $targetdir\CreateExoPSSession.ps1
$PasswordFile = "Password.txt"
$KeyFile = "AES.key"
$key = Get-Content $KeyFile
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
connect-exopssession -credential $MyCredential
#Reapply forwarding to migrated EXO mailboxes
$Users = Import-Csv $Filename
ForEach ($user in $users) {
$DeliverToMailboxAndForward = [System.Convert]::ToBoolean($User.DeliverToMailboxAndForward)
Set-Mailbox -Identity $($user.UPN) -DeliverToMailboxAndForward $DeliverTomailboxAndForward -ForwardingSmtpAddress $($user.ForwardingSmtpAddress) -whatif
}
#Kill Session and Remove Module
Get-PSSession | Remove-PSSession
Get-Module | Remove-Module
Recent Comments