Search This Blog

Tuesday, February 11, 2014

Install Timer Jobs through Powershell in SharePoint

If you need to install Timer service through Powershell in SharePoint, you can make use of the below script. This script will install custom timer job in the defined web application. Fir the purpose of testing, this script also has a function to execute the timer job. This is very helpful if you wish to execute your job immediately after installing.

Copy below code and paste it in file with extension - .ps1




#__________________________Install SharePoint Timer Jobs___________________________________

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                             VARIABLE DECLATRATION
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#

# Grab the web app URL from User
write-host "Enter the Web Application Url where you want to deploy the Timer service:"
[string]$webAppUrl = Read-Host

# Define here the Timer Job assembly name.
[string]$assemblyName = "TechPerspect.TimerJobs"

# Define here the Job Name
[string]$jobName = "TechPerspect Demo Timer Job"

# Define the class name here. (assembly name + class name of timer job)
$className = "TechPerspect.TimerJobs.DemoTimerJob"


# load the required assemblies
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][reflection.assembly]::LoadwithPartialName("Microsoft.Office.Server")

# Stop further execution of an error occured anywhere in script
$ErrorActionPreference = "Stop"


#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                 Functions
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#

# This methods restarts SharePoint Timer service (OWSTIMER)
function global:Restart-TimerServices
{
    # Restart timer services
    Write-Host "Restarting SharePoint Timer services"
    Stop-Service "SPTimerV4"
    Start-Service "SPTimerV4"
}

function global:Install-TimerJob
{
    Restart-TimerServices

    # Load job assembly
    [void][reflection.assembly]::LoadwithPartialName($assemblyName)
    # below call is commented however may be used in Powershell v3.0, since          
    # LoadwithPartialName is depreciated after powershell 2.0
    #[System.Reflection.Assembly]::Load("TechPerspect.TimerJobs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d8668901f3456325")

    $objSPSite = [Microsoft.SharePoint.SPSite]$webAppUrl
    $objWebApplication = $objSPSite.WebApplication
    # search for the Job
    $objTimerJobInstance = $objWebApplication.JobDefinitions | ? { $_.Name -like $jobName }

       # Delete the job instance if already exists
       if ($objTimerJobInstance)
    {
        Write-Host "Job [" $objTimerJobInstance.Name "] already exists. Deleting..."
       $objTimerJobInstance.Delete()
       }
      
       # Initlize new instance of timer job
    # Note that the argument List should be same, which you have defined in your 
    # timer service code for invoking the service.
    # Argument list could contain 1, 2, 3 params depending upon constructor used 
    # in your timer code
    $objTimerJobInstance = new-object $className -ArgumentList $jobName,$objWebApplication
      
       # Create a Daily Schedule
    $sched = new-object Microsoft.SharePoint.SPDailySchedule
    # setting the hour to execute at 0100 AM to 0200 AM
    $sched.BeginHour = 1
    $sched.EndHour = 2
    $objTimerJobInstance.Schedule = $sched
    $objTimerJobInstance.Update()

       # Set the schedule to the timerjob object and save the job schedule
    $objTimerJobInstance.Schedule = $sched
    $objTimerJobInstance.Update()
   
    # Dispose site
    $objSPSite.Dispose()

    Restart-TimerServices
}



#This method will execute the Timer Job.(helpful when testing the timer jobs)
function global:Execute-TimerJob
{


    # Open site
    $objSPSite = [Microsoft.SharePoint.SPSite]$webAppUrl
    $objWebApplication = $objSPSite.WebApplication

    # Content DB to execute job on
    $contentDB = $objWebApplication.ContentDatabases[0]

    # Search for the Job
    $objTimerJobInstance = $objWebApplication.JobDefinitions | ? { $_.Name -like $jobName }

    if ($objTimerJobInstance)
    {
        Write-Host "Executing Timer Job [" $objTimerJobInstance.Name "]..."
        $objTimerJobInstance.Execute($contentDB.Id)
        Write-Host "Job Executed successfully!"
    }
    else
    {
        Write-Host "Job [" $jobName "] not found"
    }
    # Dispose site
    $objSPSite.Dispose()
}



#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                        MAIN
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#


Install-TimerJob

# If you want to test the timer service, uncomment the below method call
#Write-Host "Executing the Timer Job..."
#Execute-TimerJob


2 comments:

  1. This is awesome, thank you. For many projects I had to build custom UI that were going to be used once, plus having to use SPFarm credentials. This is way better.

    A questio though, usually the timers I have to build have to have configurable parameters, which I would usually save in SPJobDefinition.Properties. How would you do that in PowerShell, is it possible?

    Thanks again, best regards.

    ReplyDelete
  2. Dear Fernando,
    You can fill the property bag for your Job instance by writing below lines of code:

    $objTimerJobInstance.Properties.Add("Key", "TP")
    $objTimerJobInstance.Update()

    ReplyDelete