INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

powershell script to forward email

powershell script to forward email

(OP)
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

RE: powershell script to forward email

rrmcguire,

Are your scripts saved as .ps1 files?

I don't have any scheduled scripts running on my Exchange servers, but I do have some on a different server. Here is how I would do it:

Because you didn't supply script names, I'll use these:
Start_ForwardMail_UserA_to_UserB.ps1 (start mail forwarding)
Stop_ForwardMail_UserA_to_UserB.ps1 (stop mail forwarding)

I. Copy script to server's local drive (I'll use c:\PowerShell_Scripts for this example)
II. Run Task Manager on the server and create the Start Forward Mail task
a. General Tab
1. Name task = Start Forward Mail From UserA to UserB
2. Set Security Options as you see fit
b. Triggers Tab (create new trigger)
1. Set timing
2. Check the "Enabled" checkbox
c. Actions (create new action)
1. Action = Start a program
2. Program/script = Powershell
3. Add arguments = .\Start_ForwardMail_UserA_to_UserB.ps1
4. Start in (Optional) = C:\PowerShell_Scripts
a. if you don't set this, II-c-3 needs to be an absolute path to the script
d. Make appropriate setting on the remaining tabs as necessary, then save the task. I left the defaults, you might want to make changes

III. Repeat the above process for the Stop Forward Mail task. Changes needed
a. II-a-1 = Stop Forward Mail From UserA to UserB
b. II-b-1 = Set timing
c. II-c-3 = .\Stop_ForwardMail_UserA_to_UserB.ps1

Good luck.


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

RE: powershell script to forward email

(OP)
I setup the above just as your specified and my mailforward schedule task runs but then ends immediately, and the mailunforward says "The last run of the task was terminated by the user(0x41306)

I have both my scripts named in the general tab

The trigger to forward is set for 2:55 PM and the trigger to unfoward is at 3:00 PM

The action is start a program: Powershell

arguments: .\mailforward.ps1
start in: C:\PowerShell_Scripts


for the unforward action:

start program: Powershell

arguments: .\mailunforward.ps1

Start in: C:\PowerShell_Scripts



Conditions and Settings nothing changed, under general for both tasks I have run with the highest privileges



My scripts are:

mailforward.ps1:

Set-Mailbox -Identity "UsertoForward" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "usertoReceive@domain.com"


mailunforward.ps1:(unforwards from UsertoForward@domain.com

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




any help would be very appreciated

RE: powershell script to forward email

The task is going to run and then stop, however the command to forward the email should be persistent. And because your script is only 1 line, it should run extremely quick. After each task runs, check to see if email is being forwarded to UserB.

CODE

Get-Mailbox -Identity "UserA" | fl DeliverToMailboxAndForward, ForwardingSmtpAddress 

Change "UserA" to be the actual account name

It should return true and the address after the mailforward script runs. It should return false and nothing after the mailunforward task runs.

For testing you might want to use a different account so you can monitor it and see if it is actually getting delivered....like a personal email account outside of your company.

Do you have both your scripts stored locally on the server in a folder called "C:\PowerShell_Scripts"?
Also check the spelling of the script name versus what you have in the task


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

RE: powershell script to forward email

(OP)
in my script I have user A as myself "Ryan McGuire" in my script as

Set-Mailbox -Identity "Ryan McGuire" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress "userb@domain.com"


should I use the above instead?

Get-Mailbox -Identity "UserA" | fl DeliverToMailboxAndForward, ForwardingSmtpAddress


as my script above works fine when run manually


Both the scripts are in the C:\PowerShell_Scripts.....as mailforward.ps1 and mailunforward.ps1

the names on the tasks are forward email and unforward email.....does that make a difference?

For the arguments in the action of the 2 tasks are they supposed to be as .\mailforward.ps1 and .\mailunforward.ps1

RE: powershell script to forward email

I think the identity needs to be the samaccount name not the display name.


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

RE: powershell script to forward email

Ryan,
I was mistaken. The display name works for me too.

For testing you can use:

CODE

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.

RE: powershell script to forward email

Quote (rrmcguire)

For the arguments in the action of the 2 tasks are they supposed to be as .\mailforward.ps1 and .\mailunforward.ps1
Yes

Do the testing and see if the scripts are working or not. If not we can try and figure out what is going wrong.


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

RE: powershell script to forward email

(OP)
When I run the above:

Get-Mailbox -Identity "Ryan McGuire" | fl DeliverToMailbxoAndForward, userb@domain.com


I get

DeliverToMailboxAndForward : False

RE: powershell script to forward email

Ryan,
Okay, so the forwarding is not set. Type the command exactly like this, don't change anything, to get the forwarding address.

CODE

Get-Mailbox -Identity "Ryan McGuire" | fl DeliverToMailboxAndForward, ForwardingSmtpAddress 

What results do you get after the mail forwarding script is run?


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

RE: powershell script to forward email

You should get a result like this

DeliverToMailboxAndForward : True
ForwardingSmtpAddress : userb@domain.com


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

RE: powershell script to forward email

(OP)
I still receive DeliverToMailboxAndForward: False

I do want to put in the actual ForwardingSmtpAddress, correct?

RE: powershell script to forward email

Quote (rrmcguire)

I do want to put in the actual ForwardingSmtpAddress, correct?

In your script, yes.
For checking the script results you want to view what the parameters are set to so use this: 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.

RE: powershell script to forward email

(OP)
where do I check the parameters at?

RE: powershell script to forward email

Run the command from within a Powershell window. I would run it on the same server upon which you are running your scripts.


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

RE: powershell script to forward email

(OP)
within Powershell or the Exchange Management Shell, within PowerShell I get:

he term 'Get-Mailbox' is not recognized as the name of
spelling of the name, or if a path was included, verif
t line:1 char:12
Get-Mailbox <<<< -Identity "Ryan McGuire" | fl Deliv
+ CategoryInfo : ObjectNotFound: (Get-Mail
+ FullyQualifiedErrorId : CommandNotFoundException



Within the Exchange Management Shelll I get:

DeliverToMailboxAndForward: False


These are both run from the Exchange Server

RE: powershell script to forward email

So, it looks like the Exchange snapins aren't loaded.

Try running this before you run the check in Powershell:
Add-PSSnapin "Microsoft.Exchange.Management.Powershell.E2010"
Add-PSSnapin "Microsoft.Exchange.Management.Powershell.Support"

If that works you will probably have to add something to the beginning of your scripts. Here is an example of your mailforward.ps1 script:

CODE -->

# mailforward.ps1

#########################################################################################################
## Function Add-Exchange-Snapins
## This function loads the snapins needed to run some scripts.  These require that the Exchange Management 
## Console tools are installed on the system running this script.
## It's called like this:  Add-Exchange-Snapins
## It returns no values
#########################################################################################################
Function Add-Exchange-Snapins{
     $mysnapins = "Microsoft.Exchange.Management.Powershell.E2010",
                  "Microsoft.Exchange.Management.Powershell.Support"		 
     ## Load snapins if they are not already loaded
     Foreach ($snapin in $mysnapins)
        {
         if((Get-PSSnapin -Name $snapin -ErrorAction SilentlyContinue) -eq $null )
	        {Add-PSSnapin $snapin}
	    }
	}
#########################################################################################################

Add-Exchange-Snapins

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


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

RE: powershell script to forward email

(OP)
I ran the two:

Add-PSSnapin "Microsoft.Exchange.Management.Powershell.E2010"
Add-PSSnapin "Microsoft.Exchange.Management.Powershell.Support"


then ran

Get-Mailbox -Identity "Ryan McGuire" | fl DeliverToMailboxAndForward, ForwardingSmtpAddress


and now within powershell get:

DeliverToMailboxAndForward : False

RE: powershell script to forward email

Ryan,
So it was the snapins not getting loaded.
Change your mailforward.ps1 script to match what I put above so that it includes the Add-Exchange-Snapins function. Then run the script through task scheduler. After that is finished. Rerun the check and see what you get. Don't run the mailunforward script, yet.

If that works, you can change the mailunforward script to include the Add-Exchange-Snapins function.


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

RE: powershell script to forward email

(OP)
just ran the task manually with the new script and it still isn't forwarding the email. I have everything as below


# mailforward.ps1

#########################################################################################################
## Function Add-Exchange-Snapins
## This function loads the snapins needed to run some scripts. These require that the Exchange Management
## Console tools are installed on the system running this script.
## It's called like this: Add-Exchange-Snapins
## It returns no values
#########################################################################################################
Function Add-Exchange-Snapins{
$mysnapins = "Microsoft.Exchange.Management.Powershell.E2010",
"Microsoft.Exchange.Management.Powershell.Support"
## Load snapins if they are not already loaded
Foreach ($snapin in $mysnapins)
{
if((Get-PSSnapin -Name $snapin -ErrorAction SilentlyContinue) -eq $null )
{Add-PSSnapin $snapin}
}
}
#########################################################################################################

Add-Exchange-Snapins

# 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"

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
DeliverToMailboxAndForward: True

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
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.
 

RE: powershell script to forward email

Quote (rrmcguire)

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.

RE: powershell script to forward email

(OP)
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

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
just running it manually after running .\mailunforward.ps1 then .\mailforward.ps1 worked :)

RE: powershell script to forward email

(OP)
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

RE: powershell script to forward email

(OP)
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

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
do you have to put anything between each line? a comma, semicolon, etc?

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
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

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
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?

RE: powershell script to forward email

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.

RE: powershell script to forward email

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close