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.
LVL 4
batmon34Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mass2612Commented:
Hi,

Try this: -

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

Running Windows PowerShell Scripts
http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx
0
BSonPoshCommented:
I think the problem is that when you schedule the task you get the standard Powershell environment. This does not (by default) include the exchange stuff.

At the beginning of your script do this

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

This will load the Exchange Snapins for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BSonPoshCommented:
Did this work for you?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

batmon34Author Commented:
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?
0
BSonPoshCommented:
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?
0
batmon34Author Commented:
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.
0
BSonPoshCommented:
So the task runs but fails? Why do you think it fails? Do you have any debug options in the script?
0
batmon34Author Commented:
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.
0
BSonPoshCommented:
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.
0
batmon34Author Commented:
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.  
0
BSonPoshCommented:
Is the schedule task running as an Admin?
Can you scrub the script and post it?
0
batmon34Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.