I got many questions about how to schedule a PowerShell script via Windows Task Scheduler. It is tricky somehow in certain conditions. So I would like to share my thoughts.
I blogged previously about this topic here and I mentioned what can happen when you enter long strings in the Create Task wizard.
In this blog post, I will try to schedule my Get-EmailCorpReport on my Windows 2012 R2 server that has Exchange 2013 on it.
First step is to go to create a folder on the C drive called MyScripts , and then drop the Get-EmailCorpReport script there.
Second thing is to make sure that the server allows PowerShell scripts to run. To do that, open PowerShell as an administrator and tun:
Then we will create a dummy PowerShell script with one line on it, to invoke the Get-CorpEmailReport with all its parameters. To do that, create a text file , and type the following inside it:
C:\myscripts\Get-CorpEmailReport_v2.4.9.ps1 -ScriptFilesPath C:\myscripts -SendMail:$true -MailFrom ExchangeTasks@contoso.com -MailTo firstname.lastname@example.org -MailServer smtp.contoso.com
Now save the file as RunMe.PS1. Make sure that the extension of the file is PS1 and not txt. Place the file inside C:\myscripts directory.
Now, open Windows Task Scheduler, create a Basic Task.
On the Action section, choose Start a program.
In the Program/Script section, type:
On the Add arguments(optional) section type:
After you finish, open the task properties, and make sure:
- Run whether user is logged on or not
- Run with highest privileges
Of course the script must be run with an account that has right to do its purpose. In this example, the account should have Exchange View Administrator, and member of the local administrators group on each Exchange server to collect WMI data.