Repository navigation
Expand file tree
/
Copy path04_Agent_Schedules.ps1
More file actions
868 lines (736 loc) · 23 KB
/
Copy path04_Agent_Schedules.ps1
File metadata and controls
868 lines (736 loc) · 23 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
<#
.SYNOPSIS
Gets the SQL Agent Schedules
.DESCRIPTION
Writes the SQL Sgent Job Schedules out to the "04 - Agent Schedules" folder, "Agent_Schedules.sql" file
.EXAMPLE
04_Agent_Schedules.ps1 localhost
.EXAMPLE
04_Agent_Schedules.ps1 server01 sa password
.Inputs
ServerName, [SQLUser], [SQLPassword]
.Outputs
.NOTES
.LINK
https://github.com/gwalkey
#>
[CmdletBinding()]
Param(
[string]$SQLInstance='localhost',
[string]$myuser,
[string]$mypass
)
# Load Common Modules and .NET Assemblies
try
{
Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop
}
catch
{
Throw('SQLTranscriptase.psm1 not found')
}
LoadSQLSMO
# Init
Set-StrictMode -Version latest;
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
Write-Host -f Yellow -b Black "04 - Agent Schedules"
Write-Output("Server: [{0}]" -f $SQLInstance)
# Create Output Folder
$fullfolderPath = "$BaseFolder\$SQLInstance\04 - Agent Schedules"
if(!(test-path -path $fullfolderPath))
{
mkdir $fullfolderPath | Out-Null
}
# Server connection check
$SQLCMD1 = "select serverproperty('productversion') as 'Version'"
try
{
if ($mypass.Length -ge 1 -and $myuser.Length -ge 1)
{
Write-Output "Testing SQL Auth"
$myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| select -ExpandProperty Version
$serverauth="sql"
}
else
{
Write-Output "Testing Windows Auth"
$myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | select -ExpandProperty Version
$serverauth = "win"
}
if($myver -ne $null)
{
Write-Output ("SQL Version: {0}" -f $myver)
}
}
catch
{
Write-Host -f red "$SQLInstance appears offline."
Set-Location $BaseFolder
exit
}
[int]$ver = GetSQLNumericalVersion $myver
# Check for Express version and exit - No Agent
$EditionSQL = "SELECT SERVERPROPERTY('Edition')"
# Run Query
if ($serverauth -eq "win")
{
try
{
$Edition = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $EditionSQL -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
else
{
try
{
$Edition = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $EditionSQL -User $myuser -Password $mypass -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
if ($Edition -ne $null )
{
if ($edition.column1 -match "Express")
{
Write-Output ("Skipping '{0}'" -f $Edition.column1)
exit
}
}
# Get Agent Schedules
$sqlCMD2 =
"
SELECT
'Exec msdb.dbo.sp_add_schedule '+
' @schedule_name='+char(39)+[name]+char(39)+
' ,@enabled=' + CASE [enabled] WHEN 1 THEN '1' WHEN 0 THEN '0' END+
' ,@freq_type=' + convert(varchar(4),[freq_type])+
' ,@freq_interval=' +convert(varchar(3),[freq_interval])+
' ,@freq_subday_type='+convert(varchar(3),[freq_subday_type])+
' ,@freq_subday_interval='+convert(varchar(3),[freq_subday_interval])+
' ,@freq_relative_interval='+convert(varchar(3),[freq_relative_interval])+
' ,@freq_recurrence_Factor='+convert(varchar(3),[freq_recurrence_factor])+
' ,@active_start_date='+convert(varchar(8),[active_start_date])+
' ,@active_end_date='+convert(varchar(8),[active_end_date])+
' ,@active_start_time='+convert(varchar(8),[active_start_time])+
' ,@active_end_time='+convert(varchar(8),[active_end_time])
FROM [msdb].[dbo].[sysschedules]
"
# Run Query
if ($serverauth -eq "win")
{
try
{
$outdata = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD2 -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
else
{
try
{
$outdata = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD2 -User $myuser -Password $mypass -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
if ($outdata -eq $null )
{
Write-Output "No Agent Schedules Found on $SQLInstance"
echo null > "$BaseFolder\$SQLInstance\04 - No Agent Schedules Found.txt"
Set-Location $BaseFolder
exit
}
New-Item "$fullfolderPath\Agent_Schedules.sql" -type file -force |Out-Null
$Outdata | Select column1 -ExpandProperty column1 | out-file "$fullfolderPath\Agent_Schedules.sql" -Encoding ascii -Append -Width 10000
Write-Output ("{0} Agent Schedules Exported" -f $outdata.count)
# ---------------------------
# Create Visual Job Schedule
# ---------------------------
<#
SQL System Tables Referenced:
sysjobs
https://msdn.microsoft.com/en-us/library/ms189817.aspx
sysschedules
https://msdn.microsoft.com/en-us/library/ms178644.aspx
sysjobschedules
https://msdn.microsoft.com/en-us/library/ms188924.aspx
In sysschedules:
Freq_Type
1 = One time only
4 = Daily
8 = Weekly
16 = Monthly
32 = Monthly, relative to freq_interval
64 = Runs when the SQL Server Agent service starts
128 = Runs when the computer is idle
freq_interval
1 (once) - freq_interval is unused (0)
4 (daily) - Every freq_interval days
8 (weekly)- freq_interval is one or more of the following:
1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday
16 (monthly) - On the freq_interval day of the month
32 (monthly, relative)- freq_interval is one of the following:
1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday
8 = Day
9 = Weekday
10 = Weekend day
64 (starts when SQL Server Agent service starts)- freq_interval is unused (0)
128 (runs when computer is idle) - freq_interval is unused (0)
freq_subday_type
1-At the specified time
2-Seconds
4-Minutes
8-Hours
freq_subday_interval
Number of freq_subday_type periods to occur between each execution of the job
freq_relative_interval
When freq_interval occurs in each month, if freq_interval is 32 (monthly relative). Can be one of the following values:
0 = freq_relative_interval is unused
1 = First
2 = Second
4 = Third
8 = Fourth
16 = Last
freq_recurrence_factor
Number of weeks or months between the scheduled execution of a job.
freq_recurrence_factor is used only if freq_type is 8, 16, or 32.
If this column contains 0, freq_recurrence_factor is unused
In sysjobschedules: "The sysjobschedules table refreshes every 20 minutes"
job_id
next_run_date
next_run_time
In sysjobs:
job_id
name
enabled
#>
$sqlCMD3 =
"
declare @AgentSked TABLE(
[Sked] nvarchar(128) NOT NULL,
[Sked_Enabled] int not null,
[Schedule_uid] uniqueidentifier not null,
[Job] nvarchar(128) NOT NULL,
[Job_Enabled] int not null,
[Frequency] varchar(45) not null,
[Freq_Interval] varchar(45) not null,
[Freq_SubDay_Type] varchar(25) not null,
[Freq_SubDay_Interval] int not null,
[StartDate] varchar(10) not null,
[StartTime] varchar(8) not null,
[StartHour] int not null,
[OriginalStartHour] int not null,
[EndHour] int not null,
[00Z] varchar(1) not null,
[01Z] varchar(1) not null,
[02Z] varchar(1) not null,
[03Z] varchar(1) not null,
[04Z] varchar(1) not null,
[05Z] varchar(1) not null,
[06Z] varchar(1) not null,
[07Z] varchar(1) not null,
[08Z] varchar(1) not null,
[09Z] varchar(1) not null,
[10Z] varchar(1) not null,
[11Z] varchar(1) not null,
[12Z] varchar(1) not null,
[13Z] varchar(1) not null,
[14Z] varchar(1) not null,
[15Z] varchar(1) not null,
[16Z] varchar(1) not null,
[17Z] varchar(1) not null,
[18Z] varchar(1) not null,
[19Z] varchar(1) not null,
[20Z] varchar(1) not null,
[21Z] varchar(1) not null,
[22Z] varchar(1) not null,
[23Z] varchar(1) not null
)
INSERT into @AgentSked
SELECT
k.[name],
k.[enabled] as 'Sked_Enabled',
k.[schedule_uid],
j.[name],
j.[enabled] as 'Job Enabled',
case [freq_type]
when 1 then 'One Shot'
when 4 then 'Daily'
when 8 then 'Weekly'
when 16 then 'Monthly'
when 32 then 'Monthly, relative to freq_interval'
when 64 then 'At Agent Startup'
when 128 then 'When Idle'
end as 'Frequency',
case [freq_type]
when 0 then 'Once'
when 1 then 'Daily'
when 4 then 'Every '+convert(varchar,[freq_interval])+' Days'
when 8 then 'Weekly on '+
case when [freq_interval] & 1=1 then 'Sun ' else '' end+
case when [freq_interval] & 2=2 then 'Mon ' else '' end+
case when [freq_interval] & 4=4 then 'Tue ' else '' end+
case when [freq_interval] & 8=8 then 'Wed ' else '' end+
case when [freq_interval] & 16=16 then 'Thu ' else '' end+
case when [freq_interval] & 32=32 then 'Fri ' else '' end+
case when [freq_interval] & 64=64 then 'Sat ' else '' end
when 16 then 'Monthly on the '+convert(varchar,[freq_interval])+' day of the Month'
when 32 then 'Monthly Relative to '+
case [freq_interval]
when 1 then 'Sun'
when 2 then 'Mon'
when 3 then 'Tue'
when 4 then 'Wed'
when 5 then 'Thu'
when 6 then 'Fri'
when 7 then 'Sat'
when 8 then 'Day'
when 9 then 'WeekDays'
when 10 then 'Weekends'
end
when 64 then 'When SQL Agent Starts'
when 128 then 'When Idle'
end as 'Freq Interval',
case [freq_subday_type]
when 0 then ''
when 1 then 'At StartTime'
when 2 then 'Every '+ convert(varchar,[freq_subday_interval]) + ' Seconds'
when 4 then 'Every '+ convert(varchar,[freq_subday_interval]) + ' Minutes'
when 8 then 'Every '+ convert(varchar,[freq_subday_interval]) + ' Hours'
end as 'Freq_Subday_Type',
[freq_subday_interval],
substring(convert(char,k.active_start_date),1,4) + '-'+substring(convert(char,k.active_start_date),5,2) +'-'+substring(convert(char,k.active_start_date),7,2) as 'StartDate',
case LEN(convert(varchar,k.active_start_time))
when 1 then '00:00:00'
when 2 then '00:00:00'
when 3 then '00:00:00'
when 4 then '00:'+SUBSTRING(convert(varchar,k.active_start_time),1,2)+':'+SUBSTRING(convert(varchar,k.active_start_time),3,2)
when 5 then '0'+SUBSTRING(convert(varchar,k.active_start_time),1,1)+':'+SUBSTRING(convert(varchar,k.active_start_time),2,2)+':'+SUBSTRING(convert(varchar,k.active_start_time),4,2)
when 6 then SUBSTRING(convert(varchar,k.active_start_time),1,2)+':'+SUBSTRING(convert(varchar,k.active_start_time),3,2)+':'+SUBSTRING(convert(varchar,k.active_start_time),5,2)
end as 'StartTime',
case LEN(convert(varchar,k.active_start_time))
when 1 then
'0'+convert(char,k.active_start_time)
when 3 then
'00'
when 4 then
'00'
when 5 then
'0'+SUBSTRING(convert(varchar,k.active_start_time),1,1)
when 6 then
SUBSTRING(convert(varchar,k.active_start_time),1,2)
end as 'StartHour',
0 as 'OriginalStartHour',
case LEN(convert(varchar,k.active_end_time))
when 1 then
'0'+convert(char,k.active_end_time)
when 3 then
'00'
when 4 then
'00'
when 5 then
'0'+SUBSTRING(convert(varchar,k.active_end_time),1,1)
when 6 then
SUBSTRING(convert(varchar,k.active_end_time),1,2)
end as 'EndHour',
[00Z] = '',
[01Z] = '',
[02Z] = '',
[03Z] = '',
[04Z] = '',
[05Z] = '',
[06Z] = '',
[07Z] = '',
[08Z] = '',
[09Z] = '',
[10Z] = '',
[11Z] = '',
[12Z] = '',
[13Z] = '',
[14Z] = '',
[15Z] = '',
[16Z] = '',
[17Z] = '',
[18Z] = '',
[19Z] = '',
[20Z] = '',
[21Z] = '',
[22Z] = '',
[23Z] = ''
FROM [msdb].[dbo].[sysschedules] k
inner join [msdb].[dbo].[sysjobschedules] b
on k.schedule_id = b.schedule_id
inner join [msdb].[dbo].[sysjobs] j
on b.job_id = j.job_id
--- Pass 1 - Mark all 'When SQL Agent Starts' jobs as Hour24 to sort low
update t
set StartHour = 99
from @AgentSked t
where t.[Frequency]='At Agent Startup'
--- Save Original StartHour for cosmetic reasons
update t
set OriginalStartHour = StartHour
from @AgentSked t
--- Pass 2 - Mark all 'At StartTime' or 'One Shot' jobs as just that one hour of the day
update t
set [00Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 0
update t
set [01Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 1
update t
set [02Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 2
update t
set [03Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 3
update t
set [04Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 4
update t
set [05Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 5
update t
set [06Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 6
update t
set [07Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 7
update t
set [08Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 8
update t
set [09Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 9
update t
set [10Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 10
update t
set [11Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 11
update t
set [12Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 12
update t
set [13Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 13
update t
set [14Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 14
update t
set [15Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 15
update t
set [16Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 16
update t
set [17Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 17
update t
set [18Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 18
update t
set [19Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 19
update t
set [20Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 20
update t
set [21Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 21
update t
set [22Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 22
update t
set [23Z] = 'x'
from @AgentSked t
where (t.[Freq_SubDay_Type]='At StartTime' or t.[Frequency]='One Shot') and t.StartHour = 23
--- Pass 3 - Get all Jobs that run only Every Hour
update t
set
t.[00Z]='x',
t.[01Z]='x',
t.[02Z]='x',
t.[03Z]='x',
t.[04Z]='x',
t.[05Z]='x',
t.[06Z]='x',
t.[07Z]='x',
t.[08Z]='x',
t.[09Z]='x',
t.[10Z]='x',
t.[11Z]='x',
t.[12Z]='x',
t.[13Z]='x',
t.[14Z]='x',
t.[15Z]='x',
t.[16Z]='x',
t.[17Z]='x',
t.[18Z]='x',
t.[19Z]='x',
t.[20Z]='x',
t.[21Z]='x',
t.[22Z]='x',
t.[23Z]='x'
FROM @AgentSked t
where t.[Freq_SubDay_Type]='Every 1 Hours'
--- Pass 3 - Get all Jobs that run 'Every X Minutes', 'Every X Seconds'
update t
set
t.[00Z]='X',
t.[01Z]='X',
t.[02Z]='X',
t.[03Z]='X',
t.[04Z]='X',
t.[05Z]='X',
t.[06Z]='X',
t.[07Z]='X',
t.[08Z]='X',
t.[09Z]='X',
t.[10Z]='X',
t.[11Z]='X',
t.[12Z]='X',
t.[13Z]='X',
t.[14Z]='X',
t.[15Z]='X',
t.[16Z]='X',
t.[17Z]='X',
t.[18Z]='X',
t.[19Z]='X',
t.[20Z]='X',
t.[21Z]='X',
t.[22Z]='X',
t.[23Z]='X'
FROM @AgentSked t
where t.[Freq_SubDay_Type] like '%Minutes' or t.[Freq_SubDay_Type] like '%Seconds'
--- Pass 4 - Get all Jobs that run Every X Hours
declare @x integer
set @x = 0
while @x>=0 and @x<=23
begin
update t
set t.[00Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=0
update t
set t.[01Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=1 and t.EndHour>=1
update t
set t.[02Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=2 and t.EndHour>=2
update t
set t.[03Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=3 and t.EndHour>=3
update t
set t.[04Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=4 and t.EndHour>=4
update t
set t.[05Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=5 and t.EndHour>=5
update t
set t.[06Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=6 and t.EndHour>=6
update t
set t.[07Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=7 and t.EndHour>=7
update t
set t.[08Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=8 and t.EndHour>=8
update t
set t.[09Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=9 and t.EndHour>=9
update t
set t.[10Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=10 and t.EndHour>=10
update t
set t.[11Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=11 and t.EndHour>=11
update t
set t.[12Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=12 and t.EndHour>=12
update t
set t.[13Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=13 and t.EndHour>=13
update t
set t.[14Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=14 and t.EndHour>=14
update t
set t.[15Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=15 and t.EndHour>=15
update t
set t.[16Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=16 and t.EndHour>=16
update t
set t.[17Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=17 and t.EndHour>=17
update t
set t.[18Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=18 and t.EndHour>=18
update t
set t.[19Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=19 and t.EndHour>=19
update t
set t.[20Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=20 and t.EndHour>=20
update t
set t.[21Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=21 and t.EndHour>=21
update t
set t.[22Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=22 and t.EndHour>=22
update t
set t.[23Z] = 'x'
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours' and StartHour=23 and t.EndHour=23
--- Move Their starttime by their interval
update t
set t.[StartHour] = t.StartHour+t.Freq_SubDay_Interval
from @AgentSked t
where t.Freq_SubDay_Type like 'Every %% Hours' and [Freq_SubDay_Type]<>'Every 1 Hours'
set @x = @x + 1
end
--- Restore Original StartHour for cosmetic reasons
update t
set StartHour = OriginalStartHour
from @AgentSked t
select * from @AgentSked
where Job_Enabled = 1
order by StartHour, Sked, Job
"
# Run Query
if ($serverauth -eq "win")
{
try
{
$LiveSkeds = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD3 -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
else
{
try
{
$LiveSkeds = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD3 -User $myuser -Password $mypass -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
# HTML CSS
$head = "<style type='text/css'>"
$head+="
table
{
Margin: 0px 0px 0px 4px;
Border: 1px solid rgb(190, 190, 190);
Font-Family: Tahoma;
Font-Size: 9pt;
Background-Color: rgb(252, 252, 252);
}
tr:hover td
{
Background-Color: rgb(150, 150, 220);
Color: rgb(255, 255, 255);
}
tr:nth-child(even)
{
Background-Color: rgb(242, 242, 242);
}
th
{
Text-Align: Left;
Color: rgb(150, 150, 220);
Padding: 1px 4px 1px 4px;
}
td
{
Vertical-Align: Top;
Padding: 1px 4px 1px 4px;
}
"
$head+="</style>"
$RunTime = Get-date
$myoutputfile4 = $FullFolderPath+"\AgentJobSchedules.html"
$myHtml1 = $LiveSkeds | select Sked, Sked_Enabled, Job, Job_Enabled, Frequency, Freq_Interval, Freq_SubDay_Type, Freq_SubDay_Interval, StartDate, StartTime, StartHour, `
00Z, 01Z, 02Z, 03Z, 04Z, 05Z, 06Z, 07Z, 08Z, 09Z, 10Z, 11Z, 12Z, 13Z, 14Z, 15Z, 16Z, 17Z, 18Z, 19Z, 20Z, 21Z, 22Z, 23Z | `
ConvertTo-Html -Fragment -as table -PreContent "<h1>Server: $SqlInstance</H1><H2>SQL Agent Job Schedules</h2>"
Convertto-Html -head $head -Body "$myHtml1" -Title "SQL Agent Job Schedules" -PostContent "<h3>Ran on : $RunTime</h3>" | Set-Content -Path $myoutputfile4
# Export the Enabled Job Schedules to CSV
$LiveSkeds | select Sked, Sked_Enabled, Job, Job_Enabled, Frequency, Freq_Interval, Freq_SubDay_Type, Freq_SubDay_Interval, StartDate, StartTime, StartHour, `
00Z, 01Z, 02Z, 03Z, 04Z, 05Z, 06Z, 07Z, 08Z, 09Z, 10Z, 11Z, 12Z, 13Z, 14Z, 15Z, 16Z, 17Z, 18Z, 19Z, 20Z, 21Z, 22Z, 23Z `
| export-CSV -LiteralPath "$fullfolderPath\AgentJobSchedules.csv"
Write-Output ("{0} Agent Jobs Exported" -f $LiveSkeds.count)
# Return to Base
set-location $BaseFolder