-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver-setup.ps1
More file actions
905 lines (584 loc) · 56.6 KB
/
server-setup.ps1
File metadata and controls
905 lines (584 loc) · 56.6 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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#Requires -RunAsAdministrator
#Requires -Modules dbatools
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$false)]
[System.String]
$sourceServer,
[Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)]
[System.String]
$destinationServer,
[Parameter(Mandatory=$false, Position=2, ValueFromPipeline=$false)]
[System.String]
$sharedLocation,
[Parameter(Mandatory=$True, Position=3, ValueFromPipeline=$false)]
[System.String]
$operatorEmail,
[Parameter(Mandatory=$True, Position=4, ValueFromPipeline=$false)]
[System.String]
$logFileDirectory,
[Parameter(Mandatory=$True, Position=5, ValueFromPipeline=$false)]
[System.String]
$dataFileDirectory,
[Parameter(Mandatory=$True, Position=6, ValueFromPipeline=$false)]
[System.String]
$adminDatabase,
[Parameter(Mandatory=$True, Position=7, ValueFromPipeline=$false)]
[System.String]
$backupLocation
)
$sqlCredential = $host.ui.PromptForCredential("Please enter your credentials", "Please enter a username and password that has admin access to the SQL server.", "", "NetBiosUserName")
$theRoot = $PSScriptRoot
$logDirectory = $theRoot + '\logs\'
$logName = 'SQL-Setup-' + $destinationServer + '-'
$logFileName = $logName + (Get-Date -f yyyy-MM-dd-HH-mm) + ".log"
$logFullPath = Join-Path $logDirectory $logFileName
$logFileLimit = (Get-Date).AddDays(-30)
$scriptPathRoot = $theRoot
$scriptPath = $scriptPathRoot + '\scripts\'
$sourceSQLConnection = Connect-DbaInstance -SqlInstance $sourceServer -SqlCredential $sqlCredential
$destinationSQLConnection = Connect-DbaInstance -SqlInstance $destinationServer -SqlCredential $sqlCredential
$availableDisks = Get-DbaDiskSpace -ComputerName $destinationSQLConnection -Credential $sqlCredential | Select-Object -Property Name
if($availableDisks | Where-Object Name -ne $logFileDirectory -or $availableDisks | Where-Object Name -ne $dataFileDirectory) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - $logFileDirectory or $dataFileDirectory was not found on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - $logFileDirectory or $dataFileDirectory was not found on $destinationServer"
return
}
$doIHaveInternet = ((Test-NetConnection www.google.com -Port 80 -InformationLevel "Detailed").TcpTestSucceeded)
$availabilityGroup = Get-DbaAvailabilityGroup -SqlInstance $destinationServer | Select-Object -ExpandProperty AvailabilityGroup
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script starting" -ForegroundColor Gray
if(-Not(Test-Path -Path $logFullPath -PathType Leaf)) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attepmting to create log file '$logFileName'" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attepmting to create log file '$logFileName'"
try {
$null = New-Item -ItemType File -Path $logFullPath -Force -ErrorAction Stop
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - The log file '$logFileName' has been created" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - The log file '$logFileName' has been created"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating log file '$logFileName'" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating log file '$logFileName'. The Error was: $error"
}
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer"
if($null -ne $availabilityGroup) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server: $destinationServer is part of $availabilityGroup availability group" -ForegroundColor Gray
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server: $destinationServer is part of $availabilityGroup availability group"
}
$logFiles = Get-ChildItem -Path $logFullPath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $logFileLimit }
$logFileCount = ($logFiles | Measure-Object).Count
if($logFileCount -gt 0) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - $logFileCount files found" -ForegroundColor Yellow
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to delete $logFileCount old log files" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to delete $logFileCount old log files $destinationServer"
try {
Get-ChildItem -Path $logFullPath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $logFileLimit } | Remove-Item -Force
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Log files sucessfully deleted" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f "yyyy-MM-dd-HH-mm") - Log files sucessfully deleted"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error deleting log files" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error deleting log files. The Error was: $error"
}
}
$dacState = Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "SELECT value_in_use FROM sys.configurations where name = 'remote admin connections'"
if($dacState.value_in_use -eq 0) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to enable the dedicated admin connection" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f "yyyy-MM-dd-HH-mm") - Attempting to enable the dedicated admin connection"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "sp_configure 'remote admin connections', 1; RECONFIGURE"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Dedicated admin connection configured sucessfully." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f "yyyy-MM-dd-HH-mm") - Dedicated admin connection configured sucessfully."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling the dedicated admin connection" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling the dedicated admin connection. The Error was: $error"
} }
$Broker = Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb'"
$MailXP = Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "SELECT value_in_use FROM sys.configurations WHERE name = 'Database Mail XPs'"
if($Broker.is_broker_enabled -eq $False -and $MailXP.value_in_use -eq 0) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to configure database mail, operator and fallback operator" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to configure database mail, operator and fallback operator"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to enable show advanced options" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to enable show advanced options"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "EXEC sp_configure 'show advanced options', '1'"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Enabling advanced featured was sucessful" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Enabling advanced featured was sucessful"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling show advanced options" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling show advanced options. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to enable database mail" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to enable database mail"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "EXEC sp_configure 'Database Mail XPs', 1"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Database mail enabled sucessfully." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Database mail enabled sucessfully."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling database mail." -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error enabling database mail. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set failsafe operator" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set failsafe operator"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "EXEC dbo.sp_MSsetalertinfo @failsafeoperator= @operator_name, @notificationmethod=1;" -SqlParameter @(operator_name = "The DBA Team") -SqlCredential $sqlCredential -Database master
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Failsafe operator sucessfully set to The DBA Team." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Failsafe operator sucessfully set to The DBA Team."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error setting failsafe operator." -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error setting failsafe operator. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set save mail in sent folder." -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set save mail in sent folder."
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "EXEC dbo.sp_set_sqlagent_properties @email_save_in_sent_folder=1;" -SqlCredential $sqlCredential -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set sent mail to save in sent folder." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set sent mail to save in sent folder."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error setting sent mail to save in sent folder." -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error setting sent mail to save in sent folder.. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to apply settings" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to apply settings"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "RECONFIGURE;"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Settings applied sucessfully." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Settings applied sucessfully."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error applying settings" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error applying settings. The Error was: $error"
}} else {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Broker already set to: $Broker" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Broker set to: $Broker"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Database Mail already set to: $MailXP" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Broker set to: $MailXP"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Enabling backup compression" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Enabling backup compression"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -Query "EXEC sp_configure 'backup compression default', 1; RECONFIGURE WITH OVERRIDE;"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Backup compression sucessfully enabled" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Backup compression sucessfully enabled"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to set backup compression, refer to the log file" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to set backup compression'. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to add trace flags" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to add trace flags"
try {
Enable-DbaTraceFlag -SqlInstance $destinationSQLConnection -TraceFlag 3226
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Trace flags set sucessfully" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Trace flags set sucessfully"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was a problem setting the trace flags" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was a problem setting the trace flags. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set the maximum memory for this instance" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set the maximum memory for this instance"
try {
Set-DbaMaxMemory -SqlInstance $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set the maximum memory for this instance" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set the maximum memory for this instance"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error setting the maximum memory for this instance" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error setting the maximum memory for this instance. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set the maxDop for this instance" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set the maxDop for this instance"
try {
Set-DbaMaxDop -SqlInstance $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set the maxDop for this instance" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully set the maxDop for this instance"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error setting the maxDop for this instance" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error setting the maxDop for this instance. The Error was: $error"
}
if($sourceServer -ne $null) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Source Server parameter was specified as $sourceServer, attempting to copy objects to $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Source Server parameter was specified as $sourceServer, attempting to copy objects to $destinationServer"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy databases from $sourceServer to $destinationServer using backup & restore to $sharedLocation" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy databases from $sourceServer to $destinationServer using backup & restore to $sharedLocation"
try {
Copy-DbaDatabase -source $sourceSQLConnection -Destination $destinationSQLConnection -BackupRestore -SharedPath $sharedLocation
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Databases sucessfully copied from $sourceServer to $destinationServer using backup & restore to $sharedLocation" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Databases sucessfully copied from $sourceServer to $destinationServer using backup & restore to $sharedLocation"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying databases from $sourceServer to $destinationServer using backup & restore to $sharedLocation" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying databases from $sourceServer to $destinationServer using backup & restore to $sharedLocation. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy agent jobs from $sourceServer to $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy agent jobs from $sourceServer to $destinationServer"
try {
Copy-DbaAgentJob -source $sourceSQLConnection -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied agent jobs from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied agent jobs from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying agent jobs from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying agent jobs from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy logins from $sourceServer to $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy logins from $sourceServer to $destinationServer"
try {
Copy-DbaLogin -source $sourceSQLConnection -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy logins from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy logins from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying logins from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying logins from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy database mail configuration from $sourceServer to $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to database mail configuration from $sourceServer to $destinationServer"
try {
Copy-DbaDbMail -source $sourceSQLConnection -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied database mail configuration from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied database mail configuration from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying database mail configuration from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying database mail configuration from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Agent Job Category from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Agent Job Category from $sourceServer to $destinationServer"
try {
Copy-DbaAgentJobCategory -Source $SourceSqlCredential -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied Agent Job Category's from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied Agent Job Category's from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying agent job category's from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying agent job category's from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy linked servers from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy linked servers from $sourceServer to $destinationServer"
try {
Copy-DbaLinkedServer -Source $SourceSqlCredential -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied linked servers from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied linked servers from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying linked servers from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying linked servers from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Custom error's from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Custom error's from $sourceServer to $destinationServer"
try {
Copy-DbaCustomError -Source $SourceSqlCredential -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied custom error's from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied custom error's from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying custom error's from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying custom error's from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Agent Alerts from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy Agent Alerts from $sourceServer to $destinationServer"
try {
Copy-DbaAgentAlert -Source $SourceSqlCredential -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied Agent Alerts from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied Agent Alerts from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying Agent Alerts from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying Agent Alerts from $sourceServer to $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy database mail operator from $sourceServer to $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to copy database mail operator from $sourceServer to $destinationServer"
try {
Copy-DbaAgentOperator -source $sourceSQLConnection -Destination $destinationSQLConnection
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied database mail operator from $sourceServer to $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully copied database mail operator from $sourceServer to $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying database mail operator from $sourceServer to $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error copying database mail operator from $sourceServer to $destinationServer. The Error was: $error"
}
} else {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - No source server was specified, attempting to create objects on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - No source server was specified, attempting to create objects on $destinationServer"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create The DBA Team operator on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create The DBA Team operator on $destinationServer"
try {
New-DbaAgentOperator -SqlInstance $destinationSQLConnection -Operator 'The DBA Team' -EmailAddress $operatorEmail
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created The DBA Team operator on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created The DBA Team operator on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating The DBA Team operator on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating The DBA Team operator on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create The DBA Team mail account on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create The DBA Team mail account on $destinationServer"
try {
$mailServer = Read-Host "Please enter your mail server"
New-DbaDbMailAccount -SqlInstance $destinationSQLConnection -Account 'The DBA Team' -EmailAddress $operatorEmail -MailServer $mailServer -DisplayName 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created The DBA Team mail account on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created The DBA Team mail account on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating The DBA Team mail account on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating The DBA Team mail account on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create the mail profile The DBA Team on $destinationserver" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create the mail profile The DBA Team on $destinationserver"
try {
New-DbaDbMailProfile -SqlInstance $destinationSQLConnection -Profile 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created the mail profile The DBA Team on $destinationserver" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created the mail profile The DBA Team on $destinationserver"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating the mail profile The DBA Team on $destinationserver" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating the mail profile The DBA Team on $destinationserver. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to add $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to add $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer"
try {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -File $scriptPath + 'scripts\' + 'alerts.sql'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully added $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully added $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $scriptPath + 'scripts\' + 'alerts.sql' to $adminDatabase on $destinationServer. The Error was: $error"
}
}
if($null -eq (Get-DbaDatabase -SqlInstance $destinationSQLConnection -Database $adminDatabase)) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $adminDatabase on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $adminDatabase on $destinationServer"
try {
New-DbaDatabase -SqlInstance $destinationSQLConnection -Name $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $adminDatabase on $destinationServer. The Error was: $error"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $adminDatabase on $destinationServer. The Error was: $error"
}
}
if($null -eq (Get-DbaDbSchema -SqlInstance $destinationSQLConnection -Database $adminDatabase -Schema 'DBA'))
{
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create schema DBA in $adminDatabase on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create schema DBA in $adminDatabase on $destinationServer"
try {
New-DbaDbSchema -SqlInstance $destinationSQLConnection -Database $adminDatabase -Schema 'DBA'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Succesfully created schema DBA in $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created schema DBA in $adminDatabase on $destinationServer. The Error was: $error"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating schema DBA in $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating schema DBA in $adminDatabase on $destinationServer. The Error was: $error"
}
}
$vendorScripts = @('olaHallengren.sql','brentScripts.sql','spWho.sql')
foreach($script in $vendorScripts) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $script in $adminDatabase" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $script in $adminDatabase"
try {
if($script -eq 'olaHallengren.sql' -and $true -eq $doIHaveInternet)
{
$mainSoloutionParams = @{
SqlInstance = $destinationSQLConnection
Database = $adminDatabase
ReplaceExisting = $true
InstallJobs = $true
LogToTable = $true
BackupLocation = $backupLocation
Verbose = $true
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Internet connection available, installing $script from the web" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Internet connection available, installing $script from the web"
try {
Install-DbaMaintenanceSolution @mainSoloutionParams -Database $adminDatabase -LogToTable -InstallJobs
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $script to $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $script to $adminDatabase on $destinationServer. The Error was: $error"
}
}
elseif($script -eq 'brenScripts.sql' -and $true -eq $doIHaveInternet) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Internet connection available, installing $script from the web" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Internet connection available, installing $script from the web"
try {
Install-DbaFirstResponderKit -SqlInstance $destinationSQLConnection -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer."
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $script to $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error adding $script to $adminDatabase on $destinationServer. The Error was: $error"
} } else {
Invoke-DbaQuery -SqlInstance $destinationSQLConnection -File $scriptPath + 'scripts\' + $script -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $script in $adminDatabase on $destinationServer."
}
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error creating $script in $adminDatabase on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - There was an error creating $script in $adminDatabase on $destinationServer. The Error was: $error"
}
}
$sqlAgentJobs = @('DBA: Send Email Alerts','_MAINT_Manage Agent Job History','DBA: Manage Agent Job History','DBA: TempDBSpaceMonitoring','DBA: WhoIsActive_WMICPUAlert','_MAINT_sp_WhoIsActive Data Collection','DBA: Check Database Mail State','DBA: DatabaseSpaceTracking','DBA: GetInstanceCPUUsage','DBA: IndexOpsExcludedDBs','DBA: IndexOpsSpaceRequirements')
$availabilityGroupJobs = @('DBA: CompareDAGAgentJobDefinitions','DBA: DatabaseSyncStatus','DBA: ReviewAgentJobConfig','_MAINT_CopyAgLogins')
#Last but not least create some jobs
if($null -ne $availabilityGroup) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - $destinationServer is part of an availability group, attempting to create ag specific jobs." -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - $destinationServer is part of an availability group, attempting to create ag specific jobs."
foreach($agJob in $availabilityGroupJobs) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $agJob on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $agJob on $destinationServer"
try {
New-DbaAgentJob -SqlInstance $destinationSQLConnection -Job $agJob -EmailLevel OnFailure -EmailOperator 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $agJob on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $agJob on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $agJob on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job step, availability check in $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job step, Primary Instance Check in $agJob on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Primary Instance Check' -Command 'SELECT ars.role_desc FROM sys.dm_hadr_availability_replica_states AS ars INNER JOIN sys.availability_groups AS ag ON ars.group_id = ag.group_id WHERE ars.is_local = 1' -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step, Primary Instance Check in $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step, Primary Instance Check in $agJob on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob on $destinationServer. The Error was: $error"
}
if($agJob -eq 'DBA: CompareDAGAgentJobDefinitions') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Compare AG Agent Job Definitions' -Command 'powershell.exe -NoLogo -NonInteractive -File "$(ESCAPE_NONE(SQLLOGDIR))\..\JOBS\CompareAGJobs.ps1" -ThisServer $(ESCAPE_NONE(SRVR)) -EmailTo ' -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Compare AG Agent Job Definitions in $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Compare AG Agent Job Definitions in $agJob on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Compare AG Agent Job Definitions in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Compare AG Agent Job Definitions in $agJob. The Error was: $error"
}
} elseif($agJob -eq '_MAINT_CopyAgLogins') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Copy Ag Logins' -Command 'powershell.exe -NoLogo -NonInteractive -File "$(ESCAPE_NONE(SQLLOGDIR))\..\JOBS\CopyAgLogins.ps1" -ThisServer $(ESCAPE_NONE(SRVR)) -LogFileFolder "$(ESCAPE_NONE(SQLLOGDIR))"' -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Copy Ag Logins $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Copy Ag Logins $agJob on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Copy Ag Logins in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Copy Ag Logins in $agJob. The Error was: $error"
}
} elseif($agJob -eq 'DBA: DatabaseSyncStatus') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $agJob on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Availability Database Sync Check' -Command 'EXEC [DBA].[AvailabilityDatabaseSyncCheck]' -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Availability Database Sync Check $agJob on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created job step Availability Database Sync Check $agJob on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Availability Database Sync Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step Availability Database Sync Check in $agJob. The Error was: $error"
}
}
}
}
foreach($job in $sqlAgentJobs) {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer"
try {
if ($job -eq '_MAINT_CycleErrorLog') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer"
try {
New-DbaAgentJob -SqlInstance $destinationSQLConnection -Job $job -EmailLevel OnFailure -EmailOperator 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job $job on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job $job on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Cycle Error Log in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Cycle Error Log in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $job -StepName 'Cycle Error Log' -Command 'Exec sys.sp_cycle_errorlog' -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Cycle Error Log in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Cycle Error Log in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Cycle Agent Error Log in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Cycle Agent Error Log in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $job -StepName 'Cycle Agent Error Log' -Command 'Exec dbo.sp_cycle_agent_errorlog' -Database msdb
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Cycle Agent Error Log in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Cycle Agent Error Log in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
}
} elseif($job -eq '_MAINT_sp_WhoIsActive Data Collection') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer"
try {
New-DbaAgentJob -SqlInstance $destinationSQLConnection -Job $job -EmailLevel OnFailure -EmailOperator 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job $job on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job $job on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Collect Who Is Active Data in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Collect Who Is Active Data in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Collect Who Is Active Data' -Command 'EXEC dbo.sp_WhoIsActive @get_transaction_info = 1,@get_outer_command = 1,@get_plans = 1,@destination_table = "DBA.DB_Administration";' -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Collect Who Is Active Data in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Collect Who Is Active Data in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Remove Old Who Is Active Data in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Remove Old Who Is Active Data in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Remove Old Who Is Active Data' -Command 'DELETE FROM DBA.WhoIsActive WHERE collection_time < DATEADD(day,-30, GETDATE());' -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Remove Old Who Is Active Data in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Remove Old Who Is Active Data in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
}
} elseif($job -eq 'DBA: DatabaseSpaceTracking') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $job on $destinationServer"
try {
New-DbaAgentJob -SqlInstance $destinationSQLConnection -Job $job -EmailLevel OnFailure -EmailOperator 'The DBA Team'
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job on $job" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job on $job. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create job $job Remove Record TempDB Space Used in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Remove Record TempDB Space Used in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Record TempDB Space Used' -Command 'EXEC [DBA].[Run_TempDBSpaceTracking]' -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Record TempDB Space Used in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Record TempDB Space Used in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
} } elseif($job -eq 'DBA: Send Email Alerts') {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Send Email Notifications in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create step Send Email Notifications in $job on $destinationServer"
try {
New-DbaAgentJobStep -SqlInstance $destinationSQLConnection -Job $agJob -StepName 'Send Email Notifications' -Command 'EXEC [DBA].[Send_Email_Notifications]' -Database $adminDatabase
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Send Email Notifications in $job on $destinationServer" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created step Send Email Notifications in $job on $destinationServer"
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating job step, Primary Instance Check in $agJob. The Error was: $error"
} } else {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to create $job on $destinationServer"
try
{
New-DbaAgentJob -SqlInstance $destinationSQLConnection -Job $job -EmailLevel OnFailure -EmailOperator 'The DBA Team'
} catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $job on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $job on $destinationServer. The Error was: $error"
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Sucessfully created $job on $destinationServer"
} } catch {
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $job on $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Error creating $job on $destinationServer. The Error was: $error"
}
}
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Don't forget to check all stored procedures and jobs created on $destinationServer to ensure they are correctly configured for your enviroment" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Don't forget to check all stored procedures and jobs created on $destinationServer to ensure they are correctly configured for your enviroment"
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script Complete" -ForegroundColor Green
Write-Host "$(Get-Date -f yyyy-MM-dd-HH-mm) - A log of actions carried out is available in $logFullPath" -ForegroundColor Yellow