By: Kathryn Bartlett
Task Scheduler is a lifesaver when it comes to deploying code late at night. Instead of having to be directly in front of your computer, dead tired, going through the simple steps up adding/deploying/upgrading wsp files, we can write a PowerShell script that will run at a specified time. There are three steps that are involved to make this work correctly:
1. Create a PowerShell script
2. Create a batch file to run this PowerShell script
3. Create a new task in Task Scheduler that runs this batch file.
Note: The deployment code in this blog is taking place in a single-server installation of SharePoint 2010.
Create a PowerShell Script
First, we need to create a PowerShell script that will contain all of our commands. Open your favorite text editor, and save the file to your C:\ drive as psscript.ps1. As our first line, we need to add the SharePoint PowerShell snap-in with the following:
What comes next would depend on what actions we need to take with our wsp. Here, I will provide the appropriate syntax for adding, deploying, upgrading, retracting, and deleting wsps:
Between each deployment command that causes recycling of app pools, I like to pause the script for a minute to ensure that everything is back in order before continuing with the next statement. To put your script to sleep for a minute, on a new line write the following:
If your deployment requires resetting a service, you can do so with the following syntax.
For the purposes of this blog, our PowerShell script will upgrade the solution package, restart the SharePoint 2010 Timer service, and call an iisreset. Below is the full C:\psscript.ps1 that will be used throughout the remainder of this blog:
Create a Batch File
Now we need to create a batch file that will run our PowerShell file, C:\psscript.ps1. This will only contain the following line:
For this blog, I’ve saved this file as C:\Deployment.bat
Create a New Task in Task Scheduler
From your SharePoint Server, open Task Scheduler under Administrator Tools. In the Action menu, select Create Task…
A dialog box to create a task will appear with multiple tabs.
Here, we need to specify a name, verify the account under which this task will run, and select what settings to run the task with.
My main reason for scheduling deployment via task scheduling is due to the fact I will not be able to be at my computer, so I always select “Run whether user is logged on or not”. Now move on to the Triggers tab.
Now we need to add a new Trigger to tell our scheduler when to execute our task. Clicking New… on the bottom left will open another dialog box.
Here, we have the ability to set what time the task should run, and set when the task should expire. After selecting appropriate times for your deployment, hit OK and continue to the Actions tab.
This tab is where we specify what the task needs to accomplish. Click New… to open another dialog box.
In this action, we are starting a program (our batch file), which we are selecting from our local file system.
Hit OK and continue to the Conditions tab.
In this tab we specify any additional conditions that would determine whether to run the task. In this instance, I want to uncheck “Start the task only if the computer is on AC power” and check “Wake the computer to run this task”.
Continue to the last tab, Settings.
In this pane, we can select any additional settings which will affect this task. In this section, I typically leave all at their default settings except for “If the task is not scheduled to run again, delete it after: ”. I check this box and select “Immediately”. I do not need to run this task again after deployment, so why keep it and clutter my list of necessary tasks?
Hit OK to save your task. You will then have to log in as the account that is scheduled to run the task. To verify your task was created, click on Task Scheduler Library in the left navigation pane.
If the task doesn’t appear immediately, click on Refresh in the right pane:
You should now see your task listed. If you click on your task, at the bottom in the middle, you can review your task settings.
By: Kathryn Bartlett