How to run PowerShell Scripts efficiently from Task Scheduler?

Hi everyone,

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.

2

Second thing is to make sure that the server allows PowerShell scripts to run. To do that, open PowerShell as an administrator and tun:

Set-ExecutionPolicy RemoteSigned

How to run PowerShell Scripts efficiently from Task Scheduler1

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 admin@contoso.com -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.

How to run PowerShell Scripts efficiently from Task Scheduler13

Now, open Windows Task Scheduler, create a Basic Task.

How to run PowerShell Scripts efficiently from Task Scheduler14

On the Action section, choose Start a program.

How to run PowerShell Scripts efficiently from Task Scheduler15

In the Program/Script section, type:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

On the Add arguments(optional) section  type:

C:\myscripts\RunMe.PS1

How to run PowerShell Scripts efficiently from Task Scheduler16

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.

How to run PowerShell Scripts efficiently from Task Scheduler18

5 comments on “How to run PowerShell Scripts efficiently from Task Scheduler?

  1. Pingback: Exchange Dashboard Organization – “Email Report” PowerShell Script | Ammar Hasayen - Blog

  2. Pingback: The most amazing “Exchange 2010 Dashboard Report” Script Monitor | Ammar Hasayen - Blog

  3. Pingback: PowerShell Automation - Get Cluster Shared Volumes Disk Space Report

  4. Pingback: Exchange Dashboard Organization – “Email Report” PowerShell Script – Azure Mechanics

  5. Pingback: The most amazing “Exchange 2010 Dashboard Report” Script Monitor – Azure Mechanics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s