Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

powershell script to forward email

Status
Not open for further replies.

rrmcguire

Programmer
Oct 13, 2010
304
US
Hello,

Im I have two powershell scripts one to forward email and one to stop the forwarding to another address, but am trying to set up a scheduled task so that the script first script runs at a certain time to start the forwarding and another to stop it.

Both scripts when run manually work perfectly, but not as a scheduled task

The script to forward the email:

Set-Mailbox -Identity "UserA" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "forward@domain.com"

the script to turn off the forwarding:

Set-Mailbox -Identity userA@domain.com -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $null


The trouble Im having is how to setup the scheduled task to get a powwershell script to run correctly. This is on an exchange 2010 environment as well

thanks for any help provided
 
What results do you get from the validation check, now?
Get-Mailbox -Identity "Ryan McGuire" | fl DeliverToMailboxAndForward, ForwardingSmtpAddress




Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
So the script ran correctly, this time. You can change the mailunforward script and it should work, too.

As to why it's not forwarding that's beyond what I can assist with and the PowerShell forum. I am curious as to why you aren't getting a response for the ForwardingSmtpAddress parameter. Do you not have permissions to set/see it?


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
so for the unforward do we just have the same as above but at the bottom put


Set-Mailbox -Identity user@domain.com -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $null



Also, Im not sure as to why the task isn't forwarding the email but when I start running the task it stays running until I manually stop it.
 
rrmcguire said:
so for the unforward do we just have the same as above but at the bottom put...

Yes.

How long do you let it run? I ask, because it may take a little longer since you now have to load the snapins. I would think a couple of minutes would be enough.

What happens if you run the script from within PowerShell; not a scheduled task?


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
when I run it manually by going to the PowerShell_scripts folder and running .\mailforward.ps1, it sits for about 20 seconds and then displays

The command completed successfully but not settings for 'path to my user' in active directory have been modified
 
So it's working. Run the mailunforward script and it should set the forwarding back to false. Then if you run the mailforward script it should run without the warning.

If you run: Get-Mailbox -Identity "user" | fl
do you see the ForwardingSMTPAddress parameter in the list? My comes up at #24 from the top of the list.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
just running it manually after running .\mailunforward.ps1 then .\mailforward.ps1 worked :)
 
I think its working now....I went into the forwarding mailbox and and there were a bunch of emails from the past, I believe you're correct as you have to wait for the snapins to load

thanks for all your help

I'll now test it out with the two tasks on schedule
 
So, everything is running correctly for one user....if I want to run this to forward about 20 people's email to that same smtp address do I just make a separate line for each person to forward their email and unforward?

thanks for all your help
 
Yes, just add another line for each mailbox to the scripts.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
do you have to put anything between each line? a comma, semicolon, etc?
 
No, just put them on their own lines like this
Code:
Set-Mailbox -Identity "UserA" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "forward@domain.com"
Set-Mailbox -Identity "UserB" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "forward@domain.com"
Set-Mailbox -Identity "UserC" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "forward@domain.com"


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
I have it set as:

# Change "UserA" to the appropriate mailbox & "forward@domain.com" to be the appropriate destination
Set-Mailbox -Identity "Ryan McGuire" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "nightcrew@meadowlarkco.com"
Set-Mailbox -Identity "Lisa Howe" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "nightcrew@meadowlarkco.com"

But will not forward while the task is running....if I have just one line for myself it works
 
Could it be that you already have the forwarding set for the first mailbox? Try disabling all the currently set forwards and then rerun the script.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
it is actually working...just wasn't waiting long enough

So If I only want this to run every Monday-Friday, do I just set it to weekly then select my days, correct?
 
If you want to run it Monday thru Friday you set the mail forward task to run every Monday morning and the mail unforward task to run every Friday night.

Once the Mail forward task runs the setting will stay until you run the mail unforward task.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Hello again,

We have in our exchange console sometimes email from users which we set to forward to other users accounts when they are gone for vacaction and such, and also have it set to deliver to their own mailbox, but when this script runs it undoes the "Deliver message to both forwarding address and mailbox"

Is there any switch or statement to add to this to keep this or do we need to apply an additional script to keep the delivering to the original mailbox

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top