Mail Forward Tool

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Brendan440
Posts: 5
Last visit: Mon Jan 29, 2024 5:09 am

Mail Forward Tool

Post by Brendan440 »

Hey all,
I am just trying to create a simple tool for my admin to make setting mail forwarders easier. We have a hybrid Exchange setup, with some users on a local 2013 server and some users on O365.

I did some research and found I could create multiple sessions, one for each Exchange server and then run the commands. At this point, testing always gives me a "Completed" message, but the forwarding never takes places. I would be grateful for anyone to look at it.
  1. $formMailForwarding_Load = {
  2.     Get-PSSession | Remove-PSSession
  3.     $UserCredential = Get-Credential
  4.     $localSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mail.server.local/PowerShell/ -Authentication Kerberos -Credential $UserCredential
  5.     Import-PSSession $localSession
  6.    
  7.     $o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $UserCredential -Authentication Basic -AllowRedirection
  8.     Import-PSSession $o365Session -Prefix O365
  9.     }
  10.  
  11. $buttonStart_Click = {
  12.     if ($connection -eq "local")
  13.     {
  14.         try
  15.         {
  16.             Invoke-Command -Session $localSession -ScriptBlock { Set-Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true } -ErrorAction Stop  
  17.         }
  18.         catch
  19.         { $labelStatus.Text = "Failed" }
  20.     }
  21.    
  22.     if ($connection -eq "o365")
  23.     {
  24.         try
  25.         { Invoke-Command -Session $o365Session -ScriptBlock { Set-O365Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true } -ErrorAction Stop }
  26.         catch
  27.         { $labelStatus.Text = "Failed" }
  28.     }
  29.     $labelStatus.Visible = $true
  30.    
  31. }
  32.  
  33. $buttonReset_Click = {
  34.     $onpremBox.Text = $null
  35.     $o365Box.Text = $null
  36.     $labelStatus.Visible = $false
  37. }
  38.  
  39.  
  40. $radiobuttonOnpremUser_CheckedChanged={
  41.     $Connection = "local"
  42.     $labelTopUser.Text = "Onprem User:"
  43.     $labelBottomUser.Text = "O365 User:"
  44. }
  45.  
  46. $radiobuttonO365User_CheckedChanged={
  47.     $Connection = "o365"
  48.     $labelTopUser.Text = "O365 User:"
  49.     $labelBottomUser.Text = "O365 User:"
  50. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Mail Forward Tool

Post by jvierra »

You would be better off doing it like this:

Code: Select all

$formMailForwarding_Load = {
    Get-PSSession | Remove-PSSession
    $UserCredential = Get-Credential
    $localSession = New-PSSession -Name LocalSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mail.server.local/PowerShell/ -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $localSession
    New-PSSession -Name O365Session -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $o365Session -Prefix O365
}

$buttonStart_Click = {
    
    if ($connection -eq 'local'){
        try{
            $session = Get-PSSession LocalSession
            Invoke-Command -Session $session -ScriptBlock { 
                Set-Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true 
            } -ErrorAction Stop   
        }
        catch{ 
            $labelStatus.Text = 'Failed'
            $labelStatus.Visible = $true
        }
    }elseif($connection -eq 'o365'){
        try{ 
            $session = Get-PSSession O365Session
            Invoke-Command -Session O365Session -ScriptBlock { 
                Set-O365Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true
            } -ErrorAction Stop 
        }
        catch{
            $labelStatus.Text = 'Failed' 
            $labelStatus.Visible = $true
       }
    }else{
        [void][System.Windows.Forms.MessageBox]::Show('No connection specified')
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Mail Forward Tool

Post by jvierra »

THe following code cannot work as arguments must be passed and not incuded:

Code: Select all

            Invoke-Command -Session O365Session -ScriptBlock { 
                Set-O365Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true
            } -ErrorAction Stop

Code: Select all

            Invoke-Command -Session O365Session -ScriptBlock { 
                Set-O365Mailbox -Identity $args[0] -ForwardingAddress $args[1] -DeliverToMailboxAndForward $true
            }  -ArgumentList $onpremBox.Text, $o365Box.Text  -ErrorAction Stop 
User avatar
Brendan440
Posts: 5
Last visit: Mon Jan 29, 2024 5:09 am

Re: Mail Forward Tool

Post by Brendan440 »

Thanks for the input. I still had some issues, I got the local forwarder working but the O365 one was still eluding me. However, it did lead me down another path, and that finally got me to where I needed to be.

Instead of multiple sessions, I create the sessions when someone chooses where the main user is located. Then can do multiple forwards then, before resetting and selecting a different location. Seems to work fine and much less headache. :D
  1. $formMailForwarding_Load = {
  2.  
  3. }
  4.  
  5.  
  6. $buttonStart_Click = {
  7.     if ($connection -eq "local")
  8.     {
  9.         try
  10.         {
  11.             Set-Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true
  12.             $labelStatus.Text = "Completed"
  13.         }
  14.         catch
  15.         {
  16.             $labelStatus.Text = "Failed"
  17.         }
  18.     }
  19.     elseif ($connection -eq "o365")
  20.     {
  21.         try
  22.         {
  23.             Set-O365Mailbox -Identity $onpremBox.Text -ForwardingAddress $o365Box.Text -DeliverToMailboxAndForward $true
  24.             $labelStatus.Text = "Completed"
  25.         }
  26.         catch
  27.         {
  28.             $labelStatus.Text = "Failed"
  29.         }
  30.     }
  31.     else
  32.     {
  33.         [void][System.Windows.Forms.MessageBox]::Show('No connection specified')
  34.     }
  35. }
  36.  
  37. $buttonReset_Click = {
  38.     Remove-PSSession
  39.     $onpremBox.Text = $null
  40.     $o365Box.Text = $null
  41.     $radiobuttonOnpremUser.Checked = $false
  42.     $radiobuttonO365User.Checked = $false
  43.     $labelStatus.Visible = $false
  44. }
  45.  
  46. $radiobuttonOnpremUser_CheckedChanged={
  47.     $global:connection = "local"
  48.     $labelStatus.Text = "Switching to local..."
  49.     $labelTopUser.Text = "Onprem User:"
  50.     $labelBottomUser.Text = "O365 User:"
  51.     $labelStatus.Visible = $true
  52.     Get-PSSession | Remove-PSSession
  53.     $UserCredential = Get-Credential
  54.     $localSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailserver.local/PowerShell/ -Authentication Kerberos -Credential $UserCredential
  55.     Import-PSSession $localSession
  56.     $labelStatus.Text = "Ready"
  57. }
  58.  
  59. $radiobuttonO365User_CheckedChanged={
  60.     $global:connection = "o365"
  61.     $labelStatus.Text = "Switching to O365..."
  62.     $labelTopUser.Text = "O365 User:"
  63.     $labelBottomUser.Text = "O365 User:"
  64.     $labelStatus.Visible = $true
  65.     Get-PSSession | Remove-PSSession
  66.     $o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $UserCredential -Authentication Basic -AllowRedirection
  67.     Import-PSSession $o365Session -Prefix O365
  68.     $labelStatus.Text = "Ready"
  69. }
  70.  
  71. $buttonClear_Click={
  72.     $onpremBox.Text = $null
  73.     $o365Box.Text = $null
  74.     $labelStatus.Visible = $false
  75. }
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked