forked from rich-howell/SQL-Server-Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-setup.ps1
More file actions
688 lines (516 loc) · 31.7 KB
/
server-setup.ps1
File metadata and controls
688 lines (516 loc) · 31.7 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
[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=3, ValueFromPipeline=$false)]
[System.String]
$logFileDirectory,
[Parameter(Mandatory=$True, Position=3, ValueFromPipeline=$false)]
[System.String]
$dataFileDirectory
)
$adminDatabase = 'DB_Administration'
$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(-15)
$scriptPathRoot = $theRoot
$scriptPath = $scriptPathRoot + '\scripts\'
$availabilityGroup = Get-DbaAvailabilityGroup -SqlInstance $destinationServer | Select-Object -ExpandProperty AvailabilityGroup
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script starting" -ForegroundColor Gray
if(-Not(Test-Path -Path $logFullPath -PathType Leaf))
{
Write-Host -Message "$(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 -Message "$(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 -Message "$(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 -Message "$(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 -Message "$(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"
}
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to delete old log files" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to delete old log files $destinationServer"
try {
Get-ChildItem -Path $logFullPath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $logFileLimit } | Remove-Item -Force
Write-Host -Message "$(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 -Message "$(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 $destinationServer -Query "SELECT value_in_use FROM sys.configurations where name = 'remote admin connections'"
Write-Host -Message "$(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"
if($dacState.value_in_use -eq 0) {
Invoke-DbaQuery -SqlInstance $destinationServer -Query "sp_configure 'remote admin connections', 1; RECONFIGURE"
Write-Host -Message "$(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."
}
$Broker = Invoke-DbaQuery -SqlInstance $destinationServer -Query "SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb'"
$MailXP = Invoke-DbaQuery -SqlInstance $destinationServer -Query "SELECT value_in_use FROM sys.configurations WHERE name = 'Database Mail XPs'"
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to set the maximum memory" -ForegroundColor Yellow
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
if($Broker.is_broker_enabled -eq $False -and $MailXP.value_in_use -eq 0) {
Write-Host -Message "$(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) - Maximum memory has been set on this server"
try {
Invoke-DbaQuery -SqlInstance $destinationServer -Query "EXEC sp_configure 'show advanced options', '1'"
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
}
catch {
Write-Host -Message "$(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"
}
Write-Host -Message "$(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) - Maximum memory has been set on this server"
try {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
Invoke-DbaQuery -SqlInstance $destinationServer -Query "EXEC sp_configure 'Database Mail XPs', 1"
}
catch {
Write-Host -Message "$(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"
}
Write-Host -Message "$(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) - Maximum memory has been set on this server"
try {
Invoke-DbaQuery -SqlInstance $destinationServer -Query "EXEC master.dbo.sp_MSsetalertinfo @failsafeoperator= @operator_name, @notificationmethod=1;" -SqlParameter @(operator_name = "The DBA Team")
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
}
catch {
Write-Host -Message "$(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"
}
Write-Host -Message "$(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) - Maximum memory has been set on this server"
try {
Invoke-DbaQuery -SqlInstance $destinationServer -Query "EXEC msdb.dbo.sp_set_sqlagent_properties @email_save_in_sent_folder=1;"
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
}
catch {
Write-Host -Message "$(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"
}
Write-Host -Message "$(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) - Maximum memory has been set on this server"
try {
Invoke-DbaQuery -SqlInstance $destinationServer -Query "RECONFIGURE;"
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Maximum memory has been set on this server"
}
catch {
Write-Host -Message "$(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"
}
}
Write-Host -Message "$(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 $destinationServer -Query "EXEC sp_configure 'backup compression default', 1; RECONFIGURE WITH OVERRIDE;"
Write-Host -Message "$(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 -Message "$(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 -Message "$(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 $destinationServer -TraceFlag 3226
Write-Host -Message "$(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 -Message "$(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 -Message "$(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 $destinationServer
Write-Host -Message "$(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 -Message "$(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 -Message "$(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 $destinationServer
Write-Host -Message "$(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 -Message "$(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 -Message "$(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) - MaxDop has been set on this server"
try {
Copy-DbaDatabase -source $sourceServer -Destination $destinationServer -BackupRestore -SharedPath $sharedLocation
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Copy-DbaAgentJob -source $sourceServer -Destination $destinationServer -BackupRestore -SharedPath $sharedLocation
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Copy-DbaLogin -source $sourceServer -Destination $destinationServer
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Copy-DbaDbMail -source $sourceServer -Destination $destinationServer
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Copy-DbaAgentOperator -source $sourceServer -Destination $destinationServer
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
} else
{
Write-Host -Message "$(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) - MaxDop has been set on this server"
try
{
New-DbaAgentOperator -SqlInstance $destinationServer -Operator 'The DBA Team' -EmailAddress $operatorEmail
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try
{
Invoke-DbaQuery -SqlInstance $destinationServer -File $scriptPath + 'scripts\' + 'alerts.sql'
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - MaxDop has been set on this server"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
if($null -eq (Get-DbaDatabase -SqlInstance $destinationServer -Database $adminDatabase))
{
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
New-DbaDatabase -SqlInstance $destinationServer -Name $adminDatabase
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
if($null -eq (Get-DbaDbSchema -SqlInstance $destinationServer -Database $adminDatabase -Schema 'DBA'))
{
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
New-DbaDbSchema -SqlInstance $destinationServer -Database $adminDatabase -Schema 'DBA'
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
if($null -eq(Get-DbaDbTable -SqlInstance $destinationServer -Database $adminDatabase -Table 'AgentJobEnabledStatus' -Schema 'dbo'))
{
$columns = @()
$columns += @{
Name = 'ID'
Type = 'int'
Identity = $true
}
$columns += @{
Name = 'AuditDate'
Type = 'datetime'
Nullable = $true
}
$columns += @{
Name = 'AGRole'
Type = 'nvarchar'
MaxLength = 60
Nullable = $true
}
$columns += @{
Name = 'JobID'
Type = 'nvarchar'
MaxLength = 36
Nullable = $true
}
$columns += @{
Name = 'JobName'
Type = 'sysname'
Nullable = $true
}
$columns += @{
Name = 'ID'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try
{
New-DbaDbTable -SqlInstance $destinationServer -Database $adminDatabase -Name 'AgentJobEnabledStatus' -Schema = 'DBA' -ColumnMap $columns
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
catch
{
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
###
if($null -eq(Get-DbaDbTable -SqlInstance $destinationServer -Database $adminDatabase -Table 'AgentJobEnabledStatus' -Schema 'dbo'))
{
# CREATE TABLE DBA.TempDBSpaceRequests(
# session_id smallint NULL,
# request_id int NULL,
# task_alloc_MB numeric(10, 1) NULL,
# task_dealloc_MB numeric(10, 1) NULL,
# task_alloc_GB numeric(10, 1) NULL,
# task_dealloc_GB numeric(10, 1) NULL,
# host nvarchar(128) NULL,
# login_name nvarchar(128) NULL,
# status nvarchar(30) NULL,
# last_request_start_time datetime NULL,
# last_request_end_time datetime NULL,
# row_count bigint NULL,
# transaction_isolation_level smallint NULL,
# query_text nvarchar(max) NULL,
# query_plan xml NULL,
# PollDate datetime NOT NULL
# ) ON PRIMARY TEXTIMAGE_ON PRIMARY
$columns = @()
$columns += @{
Name = 'session_id'
Type = 'smallint'
}
$columns += @{
Name = 'request_id'
Type = 'int'
}
$columns += @{
Name = 'task_alloc_MB'
Type = 'numeric'
Precision = 10
Nullable = $true
}
$columns += @{
Name = 'task_dealloc_MB'
Type = 'nvarchar'
MaxLength = 36
Nullable = $true
}
$columns += @{
Name = 'host'
Type = 'sysname'
Nullable = $true
}
$columns += @{
Name = 'login_name'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'status'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'last_request_start_time'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'last_request_end_time'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'row_count'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'transaction_isolation_level'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'query_text'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'query_plan'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
$columns += @{
Name = 'PollDate'
Type = 'varchar'
MaxLength = 3
Nullable = $true
}
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
New-DbaDbTable -SqlInstance $destinationServer -Database $adminDatabase -Name 'TempDBSpaceRequests' -Schema = 'DBA' -ColumnMap $columns
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
$vendorScripts = @('olaHallengren.sql','brentScripts.sql','spWho.sql')
foreach($script in $vendorScripts)
{
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Invoke-DbaQuery -SqlInstance $destinationServer -File $scriptPath + 'scripts\' + $script -Database $adminDatabase
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
} catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
$sqlAgentJobs = @('_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')
#Last but not least create some jobs
if($null -ne $availabilityGroup)
{
foreach($agJob in $availabilityGroupJobs) {
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
New-DbaAgentJob -SqlInstance $destinationServer -Job $agJob -EmailLevel OnFailure -EmailOperator 'The DBA Team'
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
try {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
New-DbaAgentJobStep -SqlInstance $destinationServer -Job $agJob -StepName 'Primary Instance Check' -Command '' -Database msdb
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
}
foreach($job in $sqlAgentJobs) {
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
New-DbaAgentJob -SqlInstance $destinationServer -Job $job -EmailLevel OnFailure -EmailOperator 'The DBA Team'
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
if($null -ne $availabilityGroup)
{
Write-Host -Message "$(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) - MaxDop has been set on this server"
try {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Green
New-DbaAgentJobStep -SqlInstance $destinationServer -Job $job -StepName 'Primary Instance Check' -Command '' -Database msdb
}
catch {
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Server being configured: $destinationServer" -ForegroundColor Red
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
}
}