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