Run Exchange Management Shell script via Task scheduler

Trying to run Exchange management Shell script on a schedule.

Scenario: I have Exchange scripts stored in E:\Scripts\Get-mailboxReport.ps1

Script is basically pulls mailbox report. If I run the script manually, I perform  below:

.\Get-MailboxReport.ps1 -server Exchange01.contoso.local -filename c:\mailboxreport.csv
which generates a csv file in the specified directory.

I want to run this script using task scheduler, also I need to send this CSV file via email.

Please give me directions on how should I approach this

Thank you
Tech ManInformation TechnologyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

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
Tech ManInformation TechnologyAuthor Commented:
First blog post is describing how to run Powershell via task scheduler. In my scenario, I have to run PowerShell for Exchange 2016( exchange management shell) plus exchange script has parameters that I have to pass.
0
J SSenior Systems AdministratorCommented:
You can create a task using the following parameters on a new action under the Action tab

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments (Optional): -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . ‘C:\yourscript.ps1’   ‘input parameters’


https://24x7itconnection.com/2015/10/22/it-snippits-schedule-exchange-2013-powershell-script-with-task-scheduler/
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

Jose Gabriel Ortega CastroCEOCommented:
Well, this is the thing.

If you open a regular PowerShell console, you won't get the regular exchange cmdlets active into that one. you just need to add a new line into your scripts that add the "snap-in".

I'm in the knowledge that you're using exchange 2016, but you can just load the pssnapin for exchange 2010, (that usually is the one I use on all my scripts because it's always present from version 2010 on).

So the solution would be quite simple:
1. Open your script "E:\Scripts\Get-mailboxReport.ps1" and add this line at the beginning :
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

or you can use any of this one:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

(Source: http://hkeylocalmachine.com/?p=180)

And about the task manager part, I did a TechNet article that you should be interested in here:
https://social.technet.microsoft.com/wiki/contents/articles/38580.configure-to-run-a-powershell-script-into-task-scheduler.aspx
0
MichelangeloConsultantCommented:
You can install exchange management tools on the workstation you'rescheduling the report on  and add at the start of the script "add-psssnapin *exchange*".

Or just schedule it on exchange server ,depending on the workload it has
0
Tech ManInformation TechnologyAuthor Commented:
Let me try this solutions, I will let you know which worked the best.

Thank you
0
Tech ManInformation TechnologyAuthor Commented:
I read all the articles and tried to come up with solution but it is not working for me.
This is what I have:
I included necessary cmdlet to my powershell to call Exchange PSSnapin.
when I run this directly from powershell it works:

.\Get-MailboxReport.ps1 -All -SendEmail -MailTo administrator@adomain.com -MailFrom exchangereports@domain.com -MailServer smtp.domain.local


this is how I set up in my Task scheduler under Action section :
Program/Script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add argument (optional): -Command "& 'E:\Scripts\Get-MailboxReport.ps1' -All -SendEmail -MailTo administrator@domain.com -MailFrom exchangereports@domain.com -MailServer smtp.domain.local"

Start in (optional): E:\Scripts

when I run this task, task is running and running and nothing happens. I have to end the task manually.

Please let me know your thoughts.
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
Powershell

From novice to tech pro — start learning today.