SharePoint 2010 custom TimerJob Installation With PowerShell

If you would like to create a custom timerjob you can follow the excellent blog of Andrew Connell.
A hidden site collection feature could do the trick, but I prefer a PowerShell script that can be executed during the installation of the solution.

#Installation script for custom timerjobs

# These variables should be set by the developer because they are environment independent
$customAssemblyName = "tvg.customtimerjob"

# Use the same name as defined in the timerjob constructor
$jobName = "Custom Job"

# Use the namespace and the classname combined.
$timerJobClassName = "tvg.customtimerjob.CustomTimerJob"

# This needs to be assigned by the admin because the script needs to beenvironment independant
#$siteUrl = "http://spf-dev/"
$siteUrl = Read-Host "Site Url";

# load the required assemblies

function Run-Init
    $global:s = [Microsoft.SharePoint.SPSite]$siteUrl
    $global:webApplication = $s.WebApplication
    $global:job = $webApplication.JobDefinitions | ? { $_.Name -like $jobName }

function Create-NewJob
    Stop-Service "SPTimerV4"
    Start-Service "SPTimerV4"
	# Delete the previous sheduled timerjob
	if ($global:job) {
	# Create a new timerjob object
    $global:job = new-object $timerJobClassName -ArgumentList $jobName,$webApplication
	# Create a new daily shedule, this can offcourse be any other available Schedule
	# More info:
    $sched = new-object Microsoft.SharePoint.SPDailySchedule
    $now = [datetime]::now.AddSeconds(10)
    $sched.BeginHour = $now.Hour
    $sched.EndHour = $now.Hour
    $sched.BeginMinute = $now.Minute
    $sched.EndMinute = $now.Minute
    $sched.beginsecond = $now.Second
    $sched.endsecond = $now.Second
	# Set the schedule to the timerjob object and save the job schedule
    $global:job.Schedule = $sched


Hope it helps,


VN:F [1.9.22_1171]
Rating: 9.7/10 (3 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 3 votes)