Configuring PowerShell to run as a Scheduled Task in Server 2008 R2

When configuring a PowerShell script to run as a scheduled task in Windows Server 2008 R2, there are a few things that you need to pay special attention to if you want to make sure that it actually runs.  Specifically, each Action in the Actions section of the Scheduled task should be configured as follows:

Action:

Start a Program

Program/Script:

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

Add arguments (optional):

-noninteractive –nologo c:\scriptpath\script.ps1

Start in (optional):

c:\scriptpath

There are a few things to note here:

  1. Do not place the script itself in the program/script filed.  Instead, you are making a call to powershell itself and then passing in the script name as a parameter. 
  2. The “-noninteractive” switch tells PowerShell that it should not present an actual shell. 
  3. The “-NoLogo” switch starts PowerShell from trying to display the copyright.  Frankly, I’m not sure this is critical, but I’ve always done it.
  4. Try and avoid placing your script in a path that contains spaces.  This means you don’t have to screw around with quotations.  This is not so much a road-block as a hurdle, but I personally like to keep things as simple as possible and I find this helps.
  5. If your script reads from or outputs to a file then make sure you don’t forget the Start In option. I like to specify this location as being the same location as my script file.  The reason I do this is that it allows me to write a script and simply specify the name of the file to read from or write to without having to specify the path in the script.  This is a matter of preference, but I find that it helps me keep things simple.
Advertisements
  1. No trackbacks yet.

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

%d bloggers like this: