{"id":827,"date":"2012-02-21T19:13:46","date_gmt":"2012-02-21T19:13:46","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=827"},"modified":"2012-02-23T13:19:47","modified_gmt":"2012-02-23T13:19:47","slug":"start-sharepoint-timer-job-script","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2012\/02\/21\/start-sharepoint-timer-job-script\/","title":{"rendered":"Start SharePoint Timer Job Script"},"content":{"rendered":"<p>I&#8217;ve written a PowerShell script that starts a SharePoint timer job, which is useful if you do a lot of work that relies on certain timers firing and don&#8217;t want to have to keep going into the Central Administration site to manually start the desired timer.  For instance, the Workflow timer fires every 15 minutes by default and although you could reduce this to 1 minute, sometimes you just want to try and force it to start.  If, like me, you don&#8217;t have the patience to wait up to a minute for it to execute \ud83d\ude42<\/p>\n<p>The script is pretty simple really, the main component being<\/p>\n<pre lang=\"powershell\">\r\njob-workflow | Start-SPTimerJob\r\n<\/pre>\n<p>Now, this on its own isn&#8217;t much use, so the following script allows you to run it and receive confirmation that the process was started.<br \/>\n<!--more--><\/p>\n<pre lang=\"powershell\">\r\n$timerjobName = \"job-workflow\"\r\n\r\n$snapinAdded = $false\r\n\r\ntry {\r\n  if ((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null ) {\r\n    $global:snapinAdded = $true\r\n    Write-Host \"Adding Sharepoint module to PowerShell\" -NoNewline\r\n    Add-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction Stop\r\n    Write-Host \" - Done.\"\r\n  }\r\n  \r\n  Write-Host \"Adding Microsoft.Sharepoint assembly\" -NoNewline\r\n  Add-Type -AssemblyName \"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\"\r\n  Write-Host \" - Done.\"\r\n  \r\n  Write-Host \"\"\r\n  Write-Host \"Starting `\"$timerJobName`\" \" -NoNewline\r\n  \r\n  $job = Get-SPTimerJob | where{$_.Name -match $timerjobName}\r\n  if ($job -eq $null)  {\r\n    Write-Host '- Timer job was not found.' -ForegroundColor Red\r\n  }\r\n  else {\r\n    $job | Start-SPTimerJob\r\n    Write-Host \"- Done.\" -ForegroundColor Green\r\n  }\r\n}\r\ncatch {\r\n  Write-Host \"\"\r\n  Write-Host \"Error starting timer job $timerJobName: \" $error[0] -ForegroundColor Red\r\n  throw\r\n  Exit 1\r\n}\r\nfinally {\r\n  if ($global:snapinAdded -eq $true) {\r\n    Write-Host \"\"\r\n    Write-Host \"Removing Sharepoint module from PowerShell\" -NoNewline\r\n    Remove-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue\r\n    Write-Host \" - Done.\"\r\n    Write-Host \"\"\r\n  }\r\n}\r\n<\/pre>\n<p>Basically, this script loads the required PowerShell snapin and Assembly, so the script can successfully run.  Next, it ensures the timer job, in this case job-workflow, exists and then it  requests that it starts.  This is purely a request and does not guarantee that it will actually run, SharePoint will determine when the timer job runs depending on the load on the farm.<\/p>\n<p>To make the script that bit easier to run, you can call it from a batch file and parameterise the timer job name to run.<\/p>\n<p>Start by adding the following to the top of the PowerShell script:<\/p>\n<pre lang=\"powershell\">\r\nparam($timerjobName)\r\n<\/pre>\n<p>Which should replace the variable declaration<\/p>\n<pre lang=\"powershell\">\r\n$timerjobName = \"job-workflow\"\r\n<\/pre>\n<p>Next, create a batch file similar to the following:<\/p>\n<pre lang=\"winbatch\">\r\n@ECHO OFF\r\n\r\nSet timerJob=job-workflow\r\n\r\npowershell.exe -Command \".\\StartTimerJob.ps1 -timerJobName \\\"%timerJob%\\\"  exit $LastExitCode\"\r\n<\/pre>\n<p>Change <em>StartTimerJob.ps1<\/em> to match the name you give your PowerShell script.<\/p>\n<p>Both files are available to download <a href='http:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/02\/Script.zip'>here<\/a>.  The files are slightly different as they add the ability to specify the URL for the Web Application the timer job runs against.  Not all timer jobs run against a Web Application, such as the Workflow Timer Job, so make sure that when you specify a URL the timer job requires one.<\/p>\n<p>To get the internal name of a timer job, take a look at the following post: <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/02\/23\/quick-tip-4\/\" title=\"Get SharePoint Timer Job Name\" target=\"_blank\">Get SharePoint Timer Job Name<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve written a PowerShell script that starts a SharePoint timer job, which is useful if you do a lot of work that relies on certain timers firing and don&#8217;t want to have to keep going into the Central Administration site &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/02\/21\/start-sharepoint-timer-job-script\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[41,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-dl","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/827"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=827"}],"version-history":[{"count":12,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/827\/revisions"}],"predecessor-version":[{"id":856,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/827\/revisions\/856"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}