Skip to content

Commit 3247d5a

Browse files
Added OlaJobsSchedule function
1 parent f8f72e4 commit 3247d5a

File tree

2 files changed

+333
-0
lines changed

2 files changed

+333
-0
lines changed

Diff for: Set-OlaJobsSchedule.ps1

+315
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
#requires -Version 1
2+
<#
3+
.SYNOPSIS
4+
Script to set some default schedules for the default jobs created by Ola Hallengrens Maintenance Solution
5+
6+
.DESCRIPTION
7+
This script will set some default job schedules for Ola Hallengrens Maintenance Solution default Jobs
8+
following the guidance on his website
9+
10+
Follow these guidelines from Ola's website https://ola.hallengren.com
11+
12+
The "One Day a week here should be a different day of the week
13+
14+
User databases:
15+
•Full backup one day per week * If using differentials otherwise daily
16+
•Differential backup all other days of the week * If required - otherwise don't schedule
17+
•Transaction log backup every hour
18+
•Integrity check one day per week
19+
•Index maintenance one day per week
20+
21+
System databases:
22+
•Full backup every day
23+
•Integrity check one day per week
24+
25+
I recommend that you run a full backup after the index maintenance. The following differential backups will then be small. I also recommend that you perform the full backup after the integrity check. Then you know that the integrity of the backup is okay.
26+
27+
28+
The one day of a week here can be the same day of the week
29+
30+
Cleanup:
31+
•sp_delete_backuphistory one day per week
32+
•sp_purge_jobhistory one day per week
33+
•CommandLog cleanup one day per week
34+
•Output file cleanup one day per week
35+
36+
.PARAMETER
37+
Server
38+
This is the connection string required to connect to the SQL Instance ServerName for a default instance, Servername\InstanceName or ServerName\InstanceName,Port
39+
.EXAMPLE
40+
Schedule-OlaJobs ServerName\InstanceName
41+
42+
43+
.NOTES
44+
Obviously requires Ola Hallengrens Maintnance Solution script to have been run first and only schedules the default jobs
45+
46+
AUTHOR: Rob Sewell sqldbawithabeard.com
47+
DATE: 1/05/2015 - Initial
48+
#>
49+
50+
function Set-OlaJobsSchedule
51+
{
52+
param([string]$Server)
53+
#Connect to server
54+
55+
$srv = New-Object -TypeName Microsoft.SQLServer.Management.SMO.Server -ArgumentList $Server
56+
$JobServer = $srv.JobServer
57+
$Jobs = $JobServer.Jobs
58+
59+
# Set Schedule for Full System DBs to once a day just before midnight
60+
61+
$Job = $Jobs|Where-Object -FilterScript {
62+
$_.Name -eq 'DatabaseBackup - SYSTEM_DATABASES - FULL'
63+
}
64+
if ($Null -eq $Job)
65+
{
66+
Write-Output -InputObject 'No Job with that name'
67+
break
68+
}
69+
else
70+
{
71+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Daily - Midnight --')
72+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
73+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
74+
$Schedule.FrequencyTypes = 'Daily'
75+
$Schedule.FrequencySubDayTypes = 'Once'
76+
$Schedule.FrequencyInterval = 1
77+
$Schedule.ActiveStartDate = Get-Date
78+
$Schedule.ActiveStartTimeOfDay = '23:46:00'
79+
$Schedule.IsEnabled = $true
80+
$Schedule.Create()
81+
}
82+
# Set Schedule for Full User DBs to once a week just after midnight on Sunday
83+
84+
$Job = $Jobs|Where-Object -FilterScript {
85+
$_.Name -eq 'DatabaseBackup - USER_DATABASES - FULL'
86+
}
87+
if ($Null -eq $Job)
88+
{
89+
Write-Output -InputObject 'No Job with that name'
90+
break
91+
}
92+
else
93+
{
94+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Sunday - Midnight ++')
95+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
96+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
97+
$Schedule.FrequencyTypes = 'Weekly'
98+
$Schedule.FrequencyRecurrenceFactor = 1
99+
$Schedule.FrequencyInterval = 1
100+
$Schedule.ActiveStartDate = Get-Date
101+
$Schedule.ActiveStartTimeOfDay = '00:16:00'
102+
$Schedule.IsEnabled = $true
103+
$Schedule.Create()
104+
}
105+
# Set Schedule for Diff User DBs to once a day just after midnight
106+
107+
$Job = $Jobs|Where-Object -FilterScript {
108+
$_.Name -eq 'DatabaseBackup - USER_DATABASES - DIFF'
109+
}
110+
if ($Null -eq $Job)
111+
{
112+
Write-Output -InputObject 'No Job with that name'
113+
break
114+
}
115+
else
116+
{
117+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Daily - Midnight ++ Not Sunday')
118+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
119+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
120+
$Schedule.FrequencyTypes = 'Weekly'
121+
$Schedule.FrequencyRecurrenceFactor = 1
122+
$Schedule.FrequencySubDayTypes = 'Once'
123+
$Schedule.FrequencyInterval = 126 # Weekdays 62 + Saturdays 64 - https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.agent.jobschedule.frequencyinterval.aspx
124+
$Schedule.ActiveStartDate = Get-Date
125+
$Schedule.ActiveStartTimeOfDay = '00:16:00'
126+
$Schedule.IsEnabled = $true
127+
$Schedule.Create()
128+
}
129+
# Set Schedule for Full System DBs to once a day just before midnight
130+
131+
$Job = $Jobs|Where-Object -FilterScript {
132+
$_.Name -eq 'DatabaseBackup - USER_DATABASES - LOG'
133+
}
134+
if ($Null -eq $Job)
135+
{
136+
Write-Output -InputObject 'No Job with that name'
137+
break
138+
}
139+
else
140+
{
141+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Hourly between 7 and 3')
142+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
143+
$Schedule.ActiveEndTimeOfDay = '02:59:59'
144+
$Schedule.FrequencyTypes = 'Daily'
145+
$Schedule.FrequencySubDayTypes = 'Hour'
146+
$Schedule.FrequencySubDayInterval = 1
147+
$Schedule.FrequencyInterval = 1
148+
$Schedule.ActiveStartDate = Get-Date
149+
$Schedule.ActiveStartTimeOfDay = '06:46:00'
150+
$Schedule.IsEnabled = $true
151+
$Schedule.Create()
152+
}
153+
154+
# Set Schedule for System DBCC to once a week just before midnight on Friday
155+
156+
$Job = $Jobs|Where-Object -FilterScript {
157+
$_.Name -eq 'DatabaseIntegrityCheck - SYSTEM_DATABASES'
158+
}
159+
if ($Null -eq $Job)
160+
{
161+
Write-Output -InputObject 'No Job with that name'
162+
break
163+
}
164+
else
165+
{
166+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Friday - Midnight --')
167+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
168+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
169+
$Schedule.FrequencyTypes = 'Weekly'
170+
$Schedule.FrequencyRecurrenceFactor = 1
171+
$Schedule.FrequencyInterval = 64
172+
$Schedule.ActiveStartDate = Get-Date
173+
$Schedule.ActiveStartTimeOfDay = '23:16:00'
174+
$Schedule.IsEnabled = $true
175+
$Schedule.Create()
176+
}
177+
# Set Schedule for User DBCC to once a week on Saturday Evening
178+
179+
$Job = $Jobs|Where-Object -FilterScript {
180+
$_.Name -eq 'DatabaseIntegrityCheck - USER_DATABASES'
181+
}
182+
if ($Null -eq $Job)
183+
{
184+
Write-Output -InputObject 'No Job with that name'
185+
break
186+
}
187+
else
188+
{
189+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Saturday - Evening')
190+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
191+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
192+
$Schedule.FrequencyTypes = 'Weekly'
193+
$Schedule.FrequencyRecurrenceFactor = 1
194+
$Schedule.FrequencyInterval = 64
195+
$Schedule.ActiveStartDate = Get-Date
196+
$Schedule.ActiveStartTimeOfDay = '20:16:00'
197+
$Schedule.IsEnabled = $true
198+
$Schedule.Create()
199+
}
200+
# Set Schedule for User IndexOptimize to once a week on Saturday Morning
201+
202+
$Job = $Jobs|Where-Object -FilterScript {
203+
$_.Name -eq 'IndexOptimize - USER_DATABASES'
204+
}
205+
if ($Null -eq $Job)
206+
{
207+
Write-Output -InputObject 'No Job with that name'
208+
break
209+
}
210+
else
211+
{
212+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Saturday - AM')
213+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
214+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
215+
$Schedule.FrequencyTypes = 'Weekly'
216+
$Schedule.FrequencyRecurrenceFactor = 1
217+
$Schedule.FrequencyInterval = 64
218+
$Schedule.ActiveStartDate = Get-Date
219+
$Schedule.ActiveStartTimeOfDay = '01:16:00'
220+
$Schedule.IsEnabled = $true
221+
$Schedule.Create()
222+
}
223+
# Set Schedule for CommandLog Cleanup to once a week on Sunday Evening
224+
225+
$Job = $Jobs|Where-Object -FilterScript {
226+
$_.Name -eq 'CommandLog Cleanup'
227+
}
228+
if ($Null -eq $Job)
229+
{
230+
Write-Output -InputObject 'No Job with that name'
231+
break
232+
}
233+
else
234+
{
235+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Sunday - Evening ')
236+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
237+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
238+
$Schedule.FrequencyTypes = 'Weekly'
239+
$Schedule.FrequencyRecurrenceFactor = 1
240+
$Schedule.FrequencyInterval = 1
241+
$Schedule.ActiveStartDate = Get-Date
242+
$Schedule.ActiveStartTimeOfDay = '19:16:00'
243+
$Schedule.IsEnabled = $true
244+
$Schedule.Create()
245+
}
246+
# Set Schedule for Output File Cleanup to once a week on Sunday Evening
247+
248+
$Job = $Jobs|Where-Object -FilterScript {
249+
$_.Name -eq 'Output File Cleanup'
250+
}
251+
if ($Null -eq $Job)
252+
{
253+
Write-Output -InputObject 'No Job with that name'
254+
break
255+
}
256+
else
257+
{
258+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Sunday - Evening ')
259+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
260+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
261+
$Schedule.FrequencyTypes = 'Weekly'
262+
$Schedule.FrequencyRecurrenceFactor = 1
263+
$Schedule.FrequencyInterval = 1
264+
$Schedule.ActiveStartDate = Get-Date
265+
$Schedule.ActiveStartTimeOfDay = '19:16:00'
266+
$Schedule.IsEnabled = $true
267+
$Schedule.Create()
268+
}
269+
# Set Schedule for sp_delete_backuphistory to once a week on Sunday Evening
270+
271+
$Job = $Jobs|Where-Object -FilterScript {
272+
$_.Name -eq 'sp_delete_backuphistory'
273+
}
274+
if ($Null -eq $Job)
275+
{
276+
Write-Output -InputObject 'No Job with that name'
277+
break
278+
}
279+
else
280+
{
281+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Sunday - Evening ')
282+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
283+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
284+
$Schedule.FrequencyTypes = 'Weekly'
285+
$Schedule.FrequencyRecurrenceFactor = 1
286+
$Schedule.FrequencyInterval = 1
287+
$Schedule.ActiveStartDate = Get-Date
288+
$Schedule.ActiveStartTimeOfDay = '19:16:00'
289+
$Schedule.IsEnabled = $true
290+
$Schedule.Create()
291+
}
292+
# Set Schedule for sp_purge_jobhistory to once a week on Sunday Evening
293+
294+
$Job = $Jobs|Where-Object -FilterScript {
295+
$_.Name -eq 'sp_purge_jobhistory'
296+
}
297+
if ($Null -eq $Job)
298+
{
299+
Write-Output -InputObject 'No Job with that name'
300+
break
301+
}
302+
else
303+
{
304+
$Schedule = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobSchedule -ArgumentList ($Job, 'Weekly Sunday - Evening ')
305+
$Schedule.ActiveEndDate = Get-Date -Month 12 -Day 31 -Year 9999
306+
$Schedule.ActiveEndTimeOfDay = '23:59:59'
307+
$Schedule.FrequencyTypes = 'Weekly'
308+
$Schedule.FrequencyRecurrenceFactor = 1
309+
$Schedule.FrequencyInterval = 1
310+
$Schedule.ActiveStartDate = Get-Date
311+
$Schedule.ActiveStartTimeOfDay = '19:16:00'
312+
$Schedule.IsEnabled = $true
313+
$Schedule.Create()
314+
}
315+
}

Diff for: Set-OlaJobsSchedule.tests.ps1

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".tests.", ".")
3+
. "$here\$sut"
4+
"$here\$sut"
5+
$Rules = Get-ScriptAnalyzerRule
6+
7+
Describe Script Analyzer Tests {
8+
Context Testing $sut for Standard Processing {
9+
foreach ($rule in $rules) {
10+
It passes the PSScriptAnalyzer Rule $rule {
11+
(Invoke-ScriptAnalyzer -Path "$here\$sut" -IncludeRule $rule.RuleName ).Count | Should Be 0
12+
}
13+
}
14+
}
15+
}
16+
Describe 'Describe Block'{
17+
Context 'First Context' {}
18+
}

0 commit comments

Comments
 (0)