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
 
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.
 
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

 
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.
 
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
 
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.
 
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.
 
rrmcguire said:
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.
 
When I run the above:

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


I get

DeliverToMailboxAndForward : False
 
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.
 
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.
 
I still receive DeliverToMailboxAndForward: False

I do want to put in the actual ForwardingSmtpAddress, correct?
 
rrmcguire said:
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.
 
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.
 
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
 
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.
 
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
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top