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
Brendan440
Posts: 5
Last visit: Mon Jan 29, 2024 5:09 am
Post
by Brendan440 » Wed Apr 18, 2018 10:33 am
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.
$formMailForwarding_Load = {
Get- PSSession | Remove- PSSession
$UserCredential = Get-Credential
$localSession = New- PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri http:// mail.server.local/ PowerShell/ - Authentication Kerberos -Credential $UserCredential
Import- PSSession $localSession
$o365Session = New- PSSession - 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
{
Invoke- Command - Session $localSession - ScriptBlock { Set- Mailbox - Identity $onpremBox .Text - ForwardingAddress $o365Box .Text - DeliverToMailboxAndForward $true } -ErrorAction Stop
}
catch
{ $labelStatus .Text = "Failed" }
}
if ( $connection -eq "o365" )
{
try
{ Invoke- Command - Session $o365Session - ScriptBlock { Set- O365Mailbox - Identity $onpremBox .Text - ForwardingAddress $o365Box .Text - DeliverToMailboxAndForward $true } -ErrorAction Stop }
catch
{ $labelStatus .Text = "Failed" }
}
$labelStatus .Visible = $true
}
$buttonReset_Click = {
$onpremBox .Text = $null
$o365Box .Text = $null
$labelStatus .Visible = $false
}
$radiobuttonOnpremUser_CheckedChanged = {
$Connection = "local"
$labelTopUser .Text = "Onprem User:"
$labelBottomUser .Text = "O365 User:"
}
$radiobuttonO365User_CheckedChanged = {
$Connection = "o365"
$labelTopUser .Text = "O365 User:"
$labelBottomUser .Text = "O365 User:"
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Wed Apr 18, 2018 12:25 pm
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
Post
by jvierra » Wed Apr 18, 2018 12:28 pm
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
Brendan440
Posts: 5
Last visit: Mon Jan 29, 2024 5:09 am
Post
by Brendan440 » Wed Apr 18, 2018 1:57 pm
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.
$formMailForwarding_Load = {
}
$buttonStart_Click = {
if ( $connection -eq "local" )
{
try
{
Set- Mailbox - Identity $onpremBox .Text - ForwardingAddress $o365Box .Text - DeliverToMailboxAndForward $true
$labelStatus .Text = "Completed"
}
catch
{
$labelStatus .Text = "Failed"
}
}
elseif ( $connection -eq "o365" )
{
try
{
Set- O365Mailbox - Identity $onpremBox .Text - ForwardingAddress $o365Box .Text - DeliverToMailboxAndForward $true
$labelStatus .Text = "Completed"
}
catch
{
$labelStatus .Text = "Failed"
}
}
else
{
[ void] [ System.Windows.Forms.MessageBox] ::Show( 'No connection specified' )
}
}
$buttonReset_Click = {
Remove- PSSession
$onpremBox .Text = $null
$o365Box .Text = $null
$radiobuttonOnpremUser .Checked = $false
$radiobuttonO365User .Checked = $false
$labelStatus .Visible = $false
}
$radiobuttonOnpremUser_CheckedChanged = {
$global :connection = "local"
$labelStatus .Text = "Switching to local..."
$labelTopUser .Text = "Onprem User:"
$labelBottomUser .Text = "O365 User:"
$labelStatus .Visible = $true
Get- PSSession | Remove- PSSession
$UserCredential = Get-Credential
$localSession = New- PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri http:// mailserver.local/ PowerShell/ - Authentication Kerberos -Credential $UserCredential
Import- PSSession $localSession
$labelStatus .Text = "Ready"
}
$radiobuttonO365User_CheckedChanged = {
$global :connection = "o365"
$labelStatus .Text = "Switching to O365..."
$labelTopUser .Text = "O365 User:"
$labelBottomUser .Text = "O365 User:"
$labelStatus .Visible = $true
Get- PSSession | Remove- PSSession
$o365Session = New- PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri https:// outlook.office365.com/ powershell- liveid/? proxymethod= rps -Credential $UserCredential - Authentication Basic - AllowRedirection
Import- PSSession $o365Session - Prefix O365
$labelStatus .Text = "Ready"
}
$buttonClear_Click = {
$onpremBox .Text = $null
$o365Box .Text = $null
$labelStatus .Visible = $false
}
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