Link to home
Start Free TrialLog in
Avatar of batmon34
batmon34Flag for United States of America

asked on

How to add Scheduled Task for a Exchange Shell Script?

Hi,

I created a Exchange PowerShell script that can check LDAP and then create new mailboxes as needed.  It works fine in the Exchange Management Shell (2007).  I want it to auto run every few hours.  I tried to add a scheduled task but it does not work.  I chose "Exchange management Shell" as the type, It added as the following:

C:\WINDOWS\system32\WINDOW~2\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"

If I want to run a script called newbox.ps1, how do I do it?

500 points.

Thank you.
Avatar of mass2612
mass2612
Flag of Australia image

Hi,

Try this: -

powershell.exe -noexit &'c:\newbox.ps1'

Running Windows PowerShell Scripts
http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did this work for you?
Avatar of batmon34

ASKER

BSonPosh,

Yes, it works.  However, I scheduled to run every four hours and it only runs whenever it feels like it.  So far for the past 2 weeks, it ran 3 times.  If I "stop" and "run" manually, it will run too.

Any idea?
Hmmm.... What do you mean by "only runs whenever it feels like it" You mean the schedule task or does the script only work occasionally?
only work occasionally.  I schedule it to run every 4 hours but it only manage to run it 3 times for the past 2 weeks.
So the task runs but fails? Why do you think it fails? Do you have any debug options in the script?
How do I tell if it failed?  The script will send email to me and I only receive 3 emails for the past 2 weeks.
I would add a line that writes to a local file just stating that the script actually ran. You could also write debug info there. It is entirely possible that the script ran, but the email failed.
You are right.  The log keeps saying

** WARNING **
      The task was forced to close since its execution time exceeded the configured maximum.
      You may want to go to the Settings page and increase the "Stop the scheduled task after" time.

If I stop the scheduled task, and then start it, it runs in seconds.  When I try to run it with scheduled time, it keeps showing this message.  The "Stop the scheduled task after" time is set 72 hours.  
Is the schedule task running as an Admin?
Can you scrub the script and post it?
It works now.  The default "Stop the scheduled task after" time is set 72 hours.  I changed it to 1 hr and now my script runs every 4 hrs as the way I wanted.  Go figure...

Thanks.