SharePoint PowerShell Timer Jobs: Run PowerShell scripts in SharePoint Timer Service context.

I’ve created a new project that combines my two primary technical passions: SharePoint 2010 and PowerShell. Smiley

The project “SharePoint PowerShell Timer Jobs” lets you create, modify and delete SharePoint timer jobs that run PowerShell scripts in the context of the SharePoint Timer Service!

It’s on Codeplex – including source code:

I hope you will love it! (If so please let me know!) – Please feel free to extend the project. If you do so please share your extensions with the community!!!

PowerShell Timer Jobs could be very useful. I’ll use it for “SharePoint Warmup”. Therefore I create a PowerShell script some months ago. This I will port to a PowerShell Timer Job and post it on my blog…

 

Now let’s have a look…

 

Here’s a screenshot of the “System Settings” page of the SharePoint 2010 Central Administration:

pic01

Use “Manage PowerShell Jobs” to create, modify or delete PowerShell jobs.

On the admin page you can select an existing timer job or select “<new>” to create a new one.

If you create a new one you have to configure it and name it. Then save the timer job. After that you’ll be able to edit the script.

In this screenshot you see an existing timer job:

Here the “Edit” button is enabled. The script below the “Edit” button is read only!

Click “Edit”… Here is what you will see:

In the Edit dialog you can enter your PowerShell script and save it.

The Edit dialog does not validate your script! It have to be valid. Or you will see errors in the history list.

At the management page you can enable or disable existing jobs using the checkbox. Don’t forget to click the “Update” button after you changed something!

At the “System Settings” page of the Central Administration you see the link “Review PowerShell Job Execution History”. Using this link you will be redirected to the history list. All outputs of all of your PowerShell jobs will be collected here.

The list will not be cleared automatically! – But… You could create a SharePoint PowerShell Timer Job for this purpose :-)

The last thing you should know: All the jobs you create are accessible at common SharePoint Job admin pages  like “Review Job definitions” and “Check job status” (Central Administration -> Monitoring)

Here you see a screenshot of the “Review Job definitions” page of SharePoint 2010:

This is a screenshot of the Job definitions detail page:

At least a screenshot of the “Check job status” page of SharePoint:

Some more details…

The PowerShell script of each timer job will be executed in a dedicated PowerShell runtime environment (“Runspace”).

For the PowerShell runspace I’ve created a PowerShell Host implementation. This implementation does not support any user interaction! Be sure your script does not need to interact with the user, e.g. for delete confirmation.

The script you enter will be surrounded with this code:

Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | out-null  
$o = Invoke-Command -ScriptBlock 
{ 
<your code> 
} 
$p = $o | out-string 
$p
 

For script development you should use this code frame too! But if you copy the script code from the development tool, e.g. Windows PowerShell ISE, to PowerShell Timer Jobs, be sure only to copy <your code>.

Furthermore you need to enable the execution of unsigned PowerShell scripts in the context of the SharePoint Timer Service. This service normally runs under the Farm account. Have a look into the “Services” management console.

Before you use the tool be sure you know what you do! – PowerShell scripts can damage your farm! BE CAREFULL!!!!I’m not responsible for any damages.

You shoud test the tool in your own environment. There could be errors in the tool!!! I’ve tested it in my dev environment, but maybe in yours it does not work properly!

Feel free to extend the tool. But if you do so please publish your code to the community.

You must not remove my name or the link to the projects homepage from any file or page of the project. – Please don’t do that. Thanks :-)

 TO USE THE PROJECT…

…you need to deploy the .WSP file to sharepoint and activate the SharePoint features in the Central Administration. The history list have to resist on the CA!!!

13 Responses to “SharePoint PowerShell Timer Jobs: Run PowerShell scripts in SharePoint Timer Service context.”

  1. Tomas

    This is really cool!

    Any chance of you blogging a little more about the components that make up the solution? Especially PowershellHost.cs and SPPowershellJobDefinition.cs. Or is there a resource you can point me at to learn from?

    Thanks,
    Tomas

    Reply
    • ikarstein

      Tomas,

      there is a chance ;-) – But it will need some time. I’ve planned an article about the “code behind”. – Thanks for your (very quick) reply only 50 minutes after publishing the project :-) – For the moment try a web search for “PSHost” or “Implement PowerShell Host”. There are some MSDN sites. But not much content…

      Kind regards
      Ingo

      Reply
  2. nickhadlee

    Great idea!

    Nice way to bring those *dirty* old scheduled tasks that run scripts into the farm environment.

    Reply
  3. Michael Herman

    When a new PS job is created, where are the job parameters (like server to run the script on) and the PS script stored? …for example, are they stored in a SP list?

    Reply
  4. Riccardo

    Hi Ingo, I like your project so much!

    I tried to setup a job and everythink worked well. Then I tried to update the schedule of the timer job from “Manage PowerShell Jobs” but I got an error. It said that a job with the same name already exists.

    I also tried to run the job manually, from “Scheduled Jobs” page, the first time I tried the job has run successfully but the second time it has run 4 times.

    These are the only error in my test, really a nice work :)

    Cheers
    Riccardo

    Reply
  5. Rahul Barua

    i am using your solution in sharepoint 2013 farm . while clicking on Edit script button i am getting javascript error “Unable to get property ‘showModalDialog’ of undefined or null reference”

    Reply

Leave a Reply