Ever need to schedule a script that you wrote with Exchange Management Shell to run on an interval? This can be done through a scheduled task PowerShell script The configuration is also a little bit tricky, because without the correct Powershell and Exchange Management settings in place this will not work. The scheduled task will say it ran, but in the end will not have executed.
Setup your Scheduled Task PowerShell Script
Setup your scheduled task and make sure the following aspects are configured as shown below.
- Open Task Scheduler
- On the General tab, ensure that the account you are using has the proper permissions to Exchange and make sure “Run whether user is logged on or not” is selected.
- On the Actions Tab, your powershell script will need to be configured to run as program by filling in the Program/Script and Add Arguments Fields. See below for the syntax for each field and adjust accordingly for your environment.
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:\powershellscriptyouwrote.ps1’
Happy Scripting and Scheduling!
That is really all that is to it! Scheduled task PowerShell scripts are a simple way to automate and successfully run the jobs you need every day.
Can you assist to get this to work -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . ‘C:\report\Get-ExchangeEnvironmentReport.ps1’ -HTMLReport ‘C:\report\exchangeenviro\report.html ‘-SendMail:$true -MailFrom:[email protected] -MailTo:[email protected] -MailServer:server.domain.local”
Typically I would incorporate anything after the .ps1 you are running into the actual ps1 file. This code should not run outside of the .ps1. Some of my Citrix powershell scripts incorporate email commands and reports, you may want to look there for some examples.
Thanks a ton for this post. I have been struggling to schedule a script and has tried several options but was not working
Glad it helped!
Bat file script including send mail parameter. This bat file work on my window server 2016 and exchange server 2016.
PowerShell.exe -noexit -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . C:\Script\Get-ExchangeEnvironmentReport.ps1 -HTMLReport C:\Script\Get-exchangeReport.html -SendMail:$true -MailFrom:[email protected] -MailTo:[email protected] -MailServer:mail.domain.com”