I’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’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’t have the patience to wait up to a minute for it to execute 🙂
The script is pretty simple really, the main component being
job-workflow | Start-SPTimerJob |
Now, this on its own isn’t much use, so the following script allows you to run it and receive confirmation that the process was started.
$timerjobName = "job-workflow" $snapinAdded = $false try { if ((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null ) { $global:snapinAdded = $true Write-Host "Adding Sharepoint module to PowerShell" -NoNewline Add-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction Stop Write-Host " - Done." } Write-Host "Adding Microsoft.Sharepoint assembly" -NoNewline Add-Type -AssemblyName "Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Write-Host " - Done." Write-Host "" Write-Host "Starting `"$timerJobName`" " -NoNewline $job = Get-SPTimerJob | where{$_.Name -match $timerjobName} if ($job -eq $null) { Write-Host '- Timer job was not found.' -ForegroundColor Red } else { $job | Start-SPTimerJob Write-Host "- Done." -ForegroundColor Green } } catch { Write-Host "" Write-Host "Error starting timer job $timerJobName: " $error[0] -ForegroundColor Red throw Exit 1 } finally { if ($global:snapinAdded -eq $true) { Write-Host "" Write-Host "Removing Sharepoint module from PowerShell" -NoNewline Remove-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue Write-Host " - Done." Write-Host "" } } |
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.
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.
Start by adding the following to the top of the PowerShell script:
param($timerjobName) |
Which should replace the variable declaration
$timerjobName = "job-workflow" |
Next, create a batch file similar to the following:
@ECHO OFF Set timerJob=job-workflow powershell.exe -Command ".\StartTimerJob.ps1 -timerJobName \"%timerJob%\" exit $LastExitCode" |
Change StartTimerJob.ps1 to match the name you give your PowerShell script.
Both files are available to download here. 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.
To get the internal name of a timer job, take a look at the following post: Get SharePoint Timer Job Name
Great article!
Helped me automate our migration process… Thanks!
Glad you found it useful 🙂