-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLMirroring.ps1
More file actions
1418 lines (1200 loc) · 72 KB
/
SQLMirroring.ps1
File metadata and controls
1418 lines (1200 loc) · 72 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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Imports The .NET Assembly To Create Userr Input Boxes
#*****************************************************************************
[Reflection.Assembly]:: LoadWithPartialName("System.Windows.Forms") | Out-Null
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Set-ExecutionPolicy RemoteSigned -Force
Add-Type -Assembly System.Web
#This Is To Get The Database Names To Be Mirrored
#*****************************************************************************
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$Databases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
$DBs = $Databases.Databases | Select Name, Size
##Grabbing All The IPs Assigned To This Server
#*****************************************************************************
$ThisIP = IPConfig -All | Select-String IPV4
$TheseIPs = ($ThisIP.Line).Substring(39) -replace "\(Preferred\)","".Trim() | Sort
##Creates Random Password For System User
$Length = 15
$NumberOfNonAlphanumericCharacters = 2
$ServerUserpword = [Web.Security.Membership]::GeneratePassword($Length,$NumberOfNonAlphanumericCharacters)
#Initial Form For IPs And Databases
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Size = New-Object System.Drawing.Size(440,310)
$Form1.ControlBox = $False
$Form1.Text = "Enter The Following Information For The Mirroring Scripts"
$Form1.StartPosition = "CenterScreen"
$OKButton1 = New-Object System.Windows.Forms.Button
$OKButton1.Location = New-Object System.Drawing.Size(125,247)
$OKButton1.Size = New-Object System.Drawing.Size(80,25)
$OKButton1.Text = "OK"
$OKButton1.Add_Click(
{
$IPValidation = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
$IncompleteField = "Please Review Following Fields: `n"
If(($InputUserBoxMirrorIP.Text.Trim() -as [Bool]) -eq $False)
{
$IncompleteField += "`nMirror Server IP"
}
If(($InputUserBoxMirrorIP.Text.Trim() -in $TheseIPs.Trim()) -eq $True)
{
$IncompleteField += "`nMirror Server IP - This Is The Principal Server IP"
}
If(($InputUserBoxWitnessIP.Text.Trim() -as [Bool]) -eq $False)
{
$IncompleteField += "`nWitness Server IP"
}
If(($InputUserBoxWitnessIP.Text.Trim() -in $TheseIPs.Trim()) -eq $True)
{
$IncompleteField += "`nWitness Server IP - This Is The Principal Server IP"
}
If($InputUserBoxMirrorIP.Text.Trim() -eq $InputUserBoxWitnessIP.Text.Trim())
{
$IncompleteField += "`nThe Mirror IP Is The Same As The Witness IP"
}
If(($InputUserBoxMirrorIP.Text.Trim() -match $IPValidation) -eq $False)
{
$IncompleteField += "`nMirror Server IP Is Invalid"
}
If(($InputUserBoxWitnessIP.Text.Trim() -match $IPValidation) -eq $False)
{
$IncompleteField += "`nWitness Server IP Is Invalid"
}
If($ObjListBoxDBList.SelectedItems.Count -eq 0)
{
$IncompleteField += "`nSelect Database(s)"
}
If(($ObjListBoxDBList.SelectedItems.Count -eq 0) -or ((($InputUserBoxMirrorIP.Text) -as [Bool]) -eq $False) -or ((($InputUserBoxWitnessIP.Text) -as [Bool]) -eq $False) -or (($InputUserBoxMirrorIP.Text.Trim() -in $TheseIPs.Trim()) -eq $True) -or (($InputUserBoxWitnessIP.Text.Trim() -in $TheseIPs.Trim()) -eq $True) -or ($InputUserBoxMirrorIP.Text.Trim() -eq $InputUserBoxWitnessIP.Text.Trim()) -or (($InputUserBoxMirrorIP.Text.Trim() -match $IPValidation) -eq $False) -or (($InputUserBoxWitnessIP.Text.Trim() -match $IPValidation) -eq $False))
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show(($IncompleteField),"Incomplete Information","OK","Error")
}
##Blocking This Off In An If Statement To Allow For Error Correcting
If(($ObjListBoxDBList.SelectedItems.Count -gt 0) -and ((($InputUserBoxMirrorIP.Text) -as [Bool]) -eq $True) -and ((($InputUserBoxWitnessIP.Text) -as [Bool]) -eq $True) -and (($InputUserBoxMirrorIP.Text.Trim() -in $TheseIPs.Trim()) -eq $False) -and (($InputUserBoxWitnessIP.Text.Trim() -in $TheseIPs.Trim()) -eq $False) -and ($InputUserBoxMirrorIP.Text.Trim() -ne $InputUserBoxWitnessIP.Text.Trim()) -and (($InputUserBoxMirrorIP.Text.Trim() -match $IPValidation) -eq $True) -and (($InputUserBoxWitnessIP.Text.Trim() -match $IPValidation) -eq $True))
{
If($Form2 -ne $Null) ##Hides Second Form For Second Pass Through
{
$Form2.Hide()
}
$Form1.Hide() ##Hides Initial Form To Allow Script To Come Back To Initial Form To Make Changes
#Assigns IP Input Values To Variables
#*****************************************************************************
##Witness And Mirror IP Addresses
$Global:MirrorIP = $InputUserBoxMirrorIP.Text.Trim()
$Global:WitnessIP = $InputUserBoxWitnessIP.Text.Trim()
##This Will Determine What IP To Use According To The Entries Made In The Mirror & Witness IP Input Boxes
$Global:PrincipalIP =
If($MirrorIP -like '10.*' -and $WitnessIP -like '10.*') ##If Both Addresses Entered Are On The Private Metwork, This Will Select The 10. Address
{
$TheseIPs.Trim() | Select -First 1
}
Else
{
If((((($MirrorIP.Split('.')[0..2]) -join '.') -eq (($WitnessIP.Split('.')[0..2]) -join '.')) -and $TheseIPs -match (($MirrorIP.Split('.')[0..2]) -join '.')) -eq $True) ##This Select The Public IP Based On The First Three Octects Incase They Are Using Cloud Networks
{
$TheseIPs.Trim() -match (($MirrorIP.Split('.')[0..2]) -join '.') | Select -First 1
}
Else
{
$TheseIPs.Trim() | Select -Last 1
}
}
#Second Form To Display Commands On Mirror And Witness To Allow Remote Access - Had To Place This Inside Of The First Form For Behaviorial Purposes
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
$Form2 = New-Object System.Windows.Forms.Form
$Form2.Size = New-Object System.Drawing.Size(600,270)
$Form2.ControlBox = $False
$Form2.Text = "Remote Enabling Commands"
$Form2.StartPosition = "CenterScreen"
$OKButton2 = New-Object System.Windows.Forms.Button
$OKButton2.Location = New-Object System.Drawing.Size(160,210)
$OKButton2.Size = New-Object System.Drawing.Size(80,25)
$OKButton2.Text = "OK"
$OKButton2.Add_Click(
{
##Testing Connection To Verify The Scripts Were Run
Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value ($MirrorIP + ',' + $WitnessIP) -Force
$RemotingPassword = ConvertTo-SecureString -AsPlainText -Force -String $ServerUserpword
$Global:RemotingCred = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist "SQL_MirrorUser",$RemotingPassword
##Creating PSSessions
$Global:MirrorSession = New-PSSession -Computername $MirrorIP -Credential $RemotingCred
$Global:WitnessSession = New-PSSession -Computername $WitnessIP -Credential $RemotingCred
##Variable For Output On Error Message
$ScriptNotRun = "The Script Was Not Successfully Run On The Following Server(s): `n"
If((($MirrorSession.Availability) -as [Bool]) -eq $False)
{
$ScriptNotRun += "`nMirror Server (double check IP)"
}
If((($WitnessSession.Availability) -as [Bool]) -eq $False)
{
$ScriptNotRun += "`nWitness Server (double check IP)"
}
If((($MirrorSession.Availability) -as [Int]) + (($WitnessSession.Availability) -as [Int]) -lt 2)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show(($ScriptNotRun),"Script Was Not Run","OK","Error")
}
Else
{
$Form2.Close()
}
})
$Form2.Controls.Add($OKButton2)
$BackButton2 = New-Object System.Windows.Forms.Button
$BackButton2.Location = New-Object System.Drawing.Size(260,210)
$BackButton2.Size = New-Object System.Drawing.Size(80,24)
$BackButton2.Text = "Back"
$BackButton2.Add_Click(
{
$Form1.show()
})
$Form2.Controls.Add($BackButton2)
$CancelButton2 = New-Object System.Windows.Forms.Button
$CancelButton2.Location = New-Object System.Drawing.Size(360,210)
$CancelButton2.Size = New-Object System.Drawing.Size(80,24)
$CancelButton2.Text = "Quit"
$CancelButton2.Add_Click(
{
Invoke-Command -Session $MirrorSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
Invoke-Command -Session $WitnessSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
$Form2.Close()
[Environment]::Exit(0)
})
$Form2.Controls.Add($CancelButton2)
##Populates The Textbox With The Commands To Be Run On The Mirror And Witness Servers (yes there is a space before the net localgroup command - idk why but powershell cuts it off when you paste it so I added a space)
$Firewalls = '
Enable-PSRemoting -Force
Set-ExecutionPolicy RemoteSigned -Force
Net User SQL_MirrorUser '''+$ServerUserpword+''' /Add /Y /Comment:"Account For SQL Mirroring." /FullName:"SQL Server User" /LogonPasswordChg:No /PasswordChg:No
WMIC Useraccount Where "Name=''SQL_MirrorUser''" Set PasswordExpires=False
Net LocalGroup Administrators SQL_MirrorUser /Add
Netsh AdvFirewall Firewall Add Rule Name="SQL_MIRROR_IPs" Dir=In Action=Allow Protocol=TCP LocalPort=Any Profile=Any Enable=Yes RemoteIP="'+$PrincipalIP+','+$InputUserBoxMirrorIP.Text+','+$InputUserBoxWitnessIP.Text+'";'+'
'
##Header Text - Remote commands
$ObjTextBoxRemote = New-Object System.Windows.Forms.Label
$ObjTextBoxRemote.Location = New-Object System.Drawing.Size(6,8)
$ObjTextBoxRemote.Size = New-Object System.Drawing.Size(580,45)
$ObjTextBoxRemote.Text="Copy and Paste These Commands in a PowerShell Window on the Mirror and Witness Server Before Proceeding!"
$ObjTextBoxRemote.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Font2 = New-Object System.Drawing.Font("Arial",11, [System.Drawing.FontStyle]::Bold)
$ObjTextBoxRemote.Font = $Font2
$Form2.Controls.Add($ObjTextBoxRemote)
##Output Box For The Textbox With The Commands Entered Above
$OutputBox2 = New-Object System.Windows.Forms.TextBox
$OutputBox2.Location = New-Object System.Drawing.Size(10,60)
$OutputBox2.Size = New-Object System.Drawing.Size(575,140)
$OutputBox2.Text = $Firewalls
$OutputBox2.MultiLine = $True
$Form2.Controls.Add($OutputBox2)
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Calls The Form To Display
#*****************************************************************************
$Form2.Add_Shown(
{
$Form2.Activate();$OutputBox2.Focus()
})
[Void] $Form2.ShowDialog()
##Closes The Initail Form
$Form1.Close()
}
})
$Form1.Controls.Add($OKButton1)
$CancelButton1 = New-Object System.Windows.Forms.Button
$CancelButton1.Location = New-Object System.Drawing.Size(225,247)
$CancelButton1.Size = New-Object System.Drawing.Size(80,24)
$CancelButton1.Text = "Quit"
$CancelButton1.Add_Click(
{
$Form1.Close()
[Environment]::Exit(0)
})
$Form1.Controls.Add($CancelButton1)
#This Is The Section Of The Text Box For Entering The IP Addresses Of The Servers
#*****************************************************************************
#*****************************************************************************
##Header Text - IP Address Heading
$ObjTextBoxIP = New-Object System.Windows.Forms.Label
$ObjTextBoxIP.Location = New-Object System.Drawing.Size(15,25)
$ObjTextBoxIP.Size = New-Object System.Drawing.Size(175,20)
$ObjTextBoxIP.Text="IP Addresses (internal preferred):"
$ObjTextBoxIP.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Form1.Controls.Add($ObjTextBoxIP)
##Input Box For Mirror Server IP
$InputUserBoxMirrorIP = New-Object System.Windows.Forms.TextBox
$InputUserBoxMirrorIP.Location = New-Object System.Drawing.Size(57,55)
$InputUserBoxMirrorIP.Size = New-Object System.Drawing.Size(120,30)
$Form1.Controls.Add($InputUserBoxMirrorIP)
##Input Box For Witness Server IP
$InputUserBoxWitnessIP = New-Object System.Windows.Forms.TextBox
$InputUserBoxWitnessIP.Location = New-Object System.Drawing.Size(280,55)
$InputUserBoxWitnessIP.Size = New-Object System.Drawing.Size(120,30)
$Form1.Controls.Add($InputUserBoxWitnessIP)
##Text - Mirror IP Input Description
$ObjTextBoxMirrorIPDesc = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorIPDesc.Location = New-Object System.Drawing.Size(22,57)
$ObjTextBoxMirrorIPDesc.Size = New-Object System.Drawing.Size(120,20)
$ObjTextBoxMirrorIPDesc.Text="Mirror:"
$Form1.Controls.Add($ObjTextBoxMirrorIPDesc)
##Text - Witness IP Input Description
$ObjTextBoxWitnessIPDesc = New-Object System.Windows.Forms.Label
$ObjTextBoxWitnessIPDesc.Location = New-Object System.Drawing.Size(232,57)
$ObjTextBoxWitnessIPDesc.Size = New-Object System.Drawing.Size(120,20)
$ObjTextBoxWitnessIPDesc.Text="Witness:"
$Form1.Controls.Add($ObjTextBoxWitnessIPDesc)
#This Is The Section That Lists The Databases To Select For Mirroring
#*****************************************************************************
#*****************************************************************************
##Selection For Databases To Mirror
$ObjListBoxDBList = New-Object System.Windows.Forms.ListBox
$ObjListBoxDBList.Location = New-Object System.Drawing.Size(90,125)
$ObjListBoxDBList.Size = New-Object System.Drawing.Size(250,20)
$ObjListBoxDBList.Height = 120
$Form1.Controls.Add($ObjListBoxDBList)
ForEach($DB in $DBs)
{
If (($DB.Name -ne "master")-and($DB.Name -ne "model")-and($DB.Name -ne "msdb")-and($DB.Name -ne "tempdb"))
{
[Void]$ObjListBoxDBList.Items.Add($DB.name)
}
$ObjListBoxDBList.SelectionMode = "MultiExtended"
}
##Text - List Of Databases To Mirror
$ObjTextBoxDBList = New-Object System.Windows.Forms.Label
$ObjTextBoxDBList.Location = New-Object System.Drawing.Size(80,100)
$ObjTextBoxDBList.Size = New-Object System.Drawing.Size(260,20)
$ObjTextBoxDBList.Text="Databases To Be Mirrored:"
$ObjTextBoxDBList.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Form1.Controls.Add($ObjTextBoxDBList)
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Calls The Form To Display
#*****************************************************************************
$Form1.Add_Shown(
{
$Form1.Activate()
})
[Void] $Form1.ShowDialog()
#Assigns Databases Selected Into Variable
#*****************************************************************************
ForEach ($ObjItemDBList In $ObjListBoxDBList.SelectedItems)
{
$X += $ObjItemDBList
}
#Creates User On Principal Server
#*****************************************************************************
Net User SQL_MirrorUser $ServerUserpword /Add /Y /Comment:"Account For SQL Mirroring." /FullName:"SQL Server User" /LogonPasswordChg:no /PasswordChg:no
WMIC UserAccount Where "Name='SQL_MirrorUser'" Set PasswordExpires=False
Net LocalGroup Administrators SQL_MirrorUser /Add
$FirewallIPs = $PrincipalIP,$InputUserBoxMirrorIP.Text,$InputUserBoxWitnessIP.Text -join ','
Netsh AdvFirewall Firewall Add Rule Name="SQL_MIRROR_IPs" Dir=In Action=Allow Protocol=TCP LocalPort=Any Profile=Any Enable=Yes RemoteIP="$FirewallIPs"
#Gathering Compatibility Settings - SQL Listening Port, Whether TCP Is Enabled, Administraor Group Access And Default Database Location On Mirror
#*****************************************************************************
##Principal
$PrincipalHostName = Hostname
$PrincipalPort = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort
$PrincipalTcpEnabled = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled
$PrincipalSQLAccess = ($Databases.Logins.Name -like '*\Administrators').Length
$Mirrors = $ObjListBox1.SelectedItems
$SelectedDatabaseSize = $DBs | Where {$_.Name -in $Mirrors}
$TotalSizeOfDatabases = ($SelectedDatabaseSize.Size | Measure-Object -Sum).Sum #Size in MB
##Mirror
New-PSSession $MirrorSession
$MirrorHostName = Invoke-Command -Session $MirrorSession -ScriptBlock {Hostname}
$MirrorPort = Invoke-Command -Session $MirrorSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort}
$MirrorTcpEnabled = Invoke-Command -Session $MirrorSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled}
$MirrorSQLAccess = Invoke-Command -Session $MirrorSession -ScriptBlock {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$MirrorDatabases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
($MirrorDatabases.Logins.Name -like '*\Administrators').Length
}
$MirrorSQLDefaultLocations = Invoke-Command -Session $MirrorSession -ScriptBlock {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$MirrorDatabases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
$DatabaseDefaults = $MirrorDatabases | Select BackupDirectory, DefaultFile, DefaultLog
}
$MirrorSQLDefaultLocations = Invoke-Command -Session $MirrorSession -ScriptBlock {$DatabaseDefaults}
##Witness
New-PSSession $WitnessSession
$WitnessPort = Invoke-Command -Session $WitnessSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort}
$WitnessTcpEnabled = Invoke-Command -Session $WitnessSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled}
$WitnessSQLAccess = Invoke-Command -Session $WitnessSession -ScriptBlock {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$Databases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
($Databases.Logins.Name -like '*\Administrators').Length
}
#Third Form For Database File Locations On Mirror Server
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
$Form3 = New-Object System.Windows.Forms.Form
$Form3.Size = New-Object System.Drawing.Size(575,270)
$Form3.ControlBox = $False
$Form3.Text = "These Are The Default Locations For The Database Files On The Mirror Server"
$Form3.StartPosition = "CenterScreen"
$OKButton3 = New-Object System.Windows.Forms.Button
$OKButton3.Location = New-Object System.Drawing.Size(193,210)
$OKButton3.Size = New-Object System.Drawing.Size(80,25)
$OKButton3.Text = "OK"
$OKButton3.Add_Click(
{
##Re-Populates Variables And Runs Remote Commands To Check For Disk Space
$Global:MirrorSQLDefaultLocations.DefaultFile = $InputUserBoxMirrorDatabaseFile.Text
$Global:MirrorSQLDefaultLocations.DefaultLog = $InputUserBoxMirrorLogFile.Text
$Global:MirrorSQLDefaultLocations.BackupDirectory = $InputUserBoxMirrorBackupFile.Text
$MirrorSQLDatabaseLocationSize = Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Get-WmiObject -Class Win32_LogicalDisk -filter "Name='$($MirrorSQLDefaultLocations.DefaultFile.Substring(0,2))'" | Select @{Name = 'FreeSpace'; Expression = {$_.FreeSpace/1MB}}} -Args $MirrorSQLDefaultLocations
$MirrorSQLDatabaseLocationFreeSpace = $MirrorSQLDatabaseLocationSize.FreeSpace ##Size In MB
$MirrorSQLBackupLocationSize = Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Get-WmiObject -Class Win32_LogicalDisk -filter "Name='$($MirrorSQLDefaultLocations.BackupDirectory.Substring(0,2))'" | Select @{Name = 'FreeSpace'; Expression = {$_.FreeSpace/1MB}}} -Args $MirrorSQLDefaultLocations
$MirrorSQLBackupLocationFreeSpace = $MirrorSQLDatabaseLocationSize.FreeSpace ##Size In MB
$LocationValidation = "The Following Path(s) Are Invalid: `n" ##Need A Variable Here To Populate Error Message Box
If((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.DefaultFile} -Args $MirrorSQLDefaultLocations) -eq $False)
{
$LocationValidation += "`nDefault Location for Database Data Files"
}
If((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.DefaultLog} -Args $MirrorSQLDefaultLocations) -eq $False)
{
$LocationValidation += "`nDefault Location for Database Log Files"
}
If((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.BackupDirectory} -Args $MirrorSQLDefaultLocations) -eq $False)
{
$LocationValidation += "`nDefault Location for Database Backup Files"}
$FreeSpaceValidation = "There Is Not Enough Disk Space In The Following Location(s): `n" ##Need Another Variable Here To Populate Error Message Box
If($TotalSizeOfDatabases -gt $MirrorSQLDatabaseLocationFreeSpace)
{
$FreeSpaceValidation += "`nThe Database Files Are Too Large To Reside On The ",$MirrorSQLDefaultLocations.DefaultFile.Substring(0,3),"`nAvailable Space: ", ("{0:N1}" -f ($MirrorSQLDatabaseLocationFreeSpace/1024)),"GB"," - Total Database Size: ", ("{0:N1}" -f ($TotalSizeOfDatabases/1024)),"GB" -join ''
}
If(($MirrorSQLDefaultLocations.DefaultFile.Substring(0,2) -eq $MirrorSQLDefaultLocations.BackupDirectory.Substring(0,2)) -and ($TotalSizeOfDatabases * 1.7) -gt $MirrorSQLDatabaseLocationFreeSpace)
{
$FreeSpaceValidation += "`n`nThe Data And Backup Files Are On The Same Directory ",$MirrorSQLDefaultLocations.DefaultFile.Substring(0,3),"`nAvailable Space: ", ("{0:N1}" -f ($MirrorSQLDatabaseLocationFreeSpace/1024)),"GB"," - Est Database and Backup Size: ", ("{0:N1}" -f (($TotalSizeOfDatabases * 1.7)/1024)),"GB" -join ''
}
If(($MirrorSQLDefaultLocations.DefaultFile.Substring(0,2) -ne $MirrorSQLDefaultLocations.BackupDirectory.Substring(0,2)) -and ($TotalSizeOfDatabases * .7) -gt $MirrorSQLBackupLocationFreeSpace)
{
$FreeSpaceValidation += "`n`nThe Backup Files May Be Too Large For The Location Specified ",$MirrorSQLDefaultLocations.BackupDirectory.Substring(0,3),"`nAvailable Space: ", ("{0:N1}" -f ($MirrorSQLBackupLocationFreeSpace/1024)),"GB"," - Est Backup Size: ", ("{0:N1}" -f (($TotalSizeOfDatabases * .7)/1024)),"GB" -join ''
}
If ((((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.DefaultFile} -Args $MirrorSQLDefaultLocations) -as [Int]) + ((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.DefaultLog} -Args $MirrorSQLDefaultLocations) -as [Int]) + ((Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorSQLDefaultLocations) Test-Path $MirrorSQLDefaultLocations.BackupDirectory} -Args $MirrorSQLDefaultLocations) -as [Int])) -lt 3)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show(($LocationValidation),"Invalid Path","OK","Error")
}
ElseIf (($TotalSizeOfDatabases -gt $MirrorSQLDatabaseLocationFreeSpace) -or (($MirrorSQLDefaultLocations.DefaultFile.Substring(0,2) -eq $MirrorSQLDefaultLocations.BackupDirectory.Substring(0,2)) -and ($TotalSizeOfDatabases * 1.7) -gt $MirrorSQLDatabaseLocationFreeSpace) -or (($MirrorSQLDefaultLocations.DefaultFile.Substring(0,2) -ne $MirrorSQLDefaultLocations.BackupDirectory.Substring(0,2)) -and ($TotalSizeOfDatabases * .7) -gt $MirrorSQLBackupLocationFreeSpace))
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show(($FreeSpaceValidation),"Insufficient Space","OK","Error")
}
Else
{
$Form3.Close()
}
})
$Form3.Controls.Add($OKButton3)
$CancelButton3 = New-Object System.Windows.Forms.Button
$CancelButton3.Location = New-Object System.Drawing.Size(293,210)
$CancelButton3.Size = New-Object System.Drawing.Size(80,24)
$CancelButton3.Text = "Quit"
$CancelButton3.Add_Click(
{
Net User SQL_MirrorUser /Delete
Invoke-Command -Session $MirrorSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
Invoke-Command -Session $WitnessSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
$Form3.Close()
[Environment]::Exit(0)
})
$Form3.Controls.Add($CancelButton3)
#This Section Is For The Input Boxes For Default File Locations For SQL On Mirror Server
#*****************************************************************************
#*****************************************************************************
##Header Text - SQL File Locations On Mirror
$ObjTextBoxMirrorSQLFiles = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorSQLFiles.Location = New-Object System.Drawing.Size(22,25)
$ObjTextBoxMirrorSQLFiles.Size = New-Object System.Drawing.Size(368,20)
$ObjTextBoxMirrorSQLFiles.Text="Default File Locations For SQL Server On Mirror Server:"
$Form3.Controls.Add($ObjTextBoxMirrorSQLFiles)
##Input Box For Default Database File Location
$InputUserBoxMirrorDatabaseFile = New-Object System.Windows.Forms.TextBox
$InputUserBoxMirrorDatabaseFile.Location = New-Object System.Drawing.Size(92,55)
$InputUserBoxMirrorDatabaseFile.Size = New-Object System.Drawing.Size(450,30)
$InputUserBoxMirrorDatabaseFile.Text = $MirrorSQLDefaultLocations.DefaultFile
$Form3.Controls.Add($InputUserBoxMirrorDatabaseFile)
##Input Box For Default Log File Location
$InputUserBoxMirrorLogFile = New-Object System.Windows.Forms.TextBox
$InputUserBoxMirrorLogFile.Location = New-Object System.Drawing.Size(92,85)
$InputUserBoxMirrorLogFile.Size = New-Object System.Drawing.Size(450,30)
$InputUserBoxMirrorLogFile.Text = $MirrorSQLDefaultLocations.DefaultLog
$Form3.Controls.Add($InputUserBoxMirrorLogFile)
##Input Box For Default Backup File Location
$InputUserBoxMirrorBackupFile = New-Object System.Windows.Forms.TextBox
$InputUserBoxMirrorBackupFile.Location = New-Object System.Drawing.Size(92,115)
$InputUserBoxMirrorBackupFile.Size = New-Object System.Drawing.Size(450,30)
$InputUserBoxMirrorBackupFile.Text = $MirrorSQLDefaultLocations.BackupDirectory
$Form3.Controls.Add($InputUserBoxMirrorBackupFile)
##Text - Default Database File Location Input Description
$ObjTextBoxMirrorDatabaseFileDesc = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorDatabaseFileDesc.Location = New-Object System.Drawing.Size(22,57)
$ObjTextBoxMirrorDatabaseFileDesc.Size = New-Object System.Drawing.Size(120,20)
$ObjTextBoxMirrorDatabaseFileDesc.Text="Data Files:"
$Form3.Controls.Add($ObjTextBoxMirrorDatabaseFileDesc)
##Text - Default Log File Location Input Description
$ObjTextBoxMirrorLogFileDesc = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorLogFileDesc.Location = New-Object System.Drawing.Size(22,87)
$ObjTextBoxMirrorLogFileDesc.Size = New-Object System.Drawing.Size(120,20)
$ObjTextBoxMirrorLogFileDesc.Text="Log Files:"
$Form3.Controls.Add($ObjTextBoxMirrorLogFileDesc)
##Text - Default Backup File Location Input Description
$ObjTextBoxMirrorBackupFileDesc = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorBackupFileDesc.Location = New-Object System.Drawing.Size(22,117)
$ObjTextBoxMirrorBackupFileDesc.Size = New-Object System.Drawing.Size(120,20)
$ObjTextBoxMirrorBackupFileDesc.Text="Backup Files:"
$Form3.Controls.Add($ObjTextBoxMirrorBackupFileDesc)
##Text - Informational Blurb Regarding Changing These Values Manually
$ObjTextBoxMirrorLocationUpdateInfo = New-Object System.Windows.Forms.Label
$ObjTextBoxMirrorLocationUpdateInfo.Location = New-Object System.Drawing.Size(27,162)
$ObjTextBoxMirrorLocationUpdateInfo.Size = New-Object System.Drawing.Size(540,40)
$ObjTextBoxMirrorLocationUpdateInfo.Text="***Please Note Changing These Values Will Only Affect The Location Of These Databases Being Restored For Mirroring; It Will Not Change The Default Location Properties For SQL Server"
$Font3 = New-Object System.Drawing.Font("Arial",7.5)
$ObjTextBoxMirrorLocationUpdateInfo.Font = $Font3
$Form3.Controls.Add($ObjTextBoxMirrorLocationUpdateInfo)
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Calls The Form To Display
#*****************************************************************************
$Form3.Add_Shown(
{
$Form3.Activate()
})
[Void] $Form3.ShowDialog()
#Formatting Database Default Entries
#*****************************************************************************
##Default File
$MirrorSQLDefaultLocations.DefaultFile =
If (($MirrorSQLDefaultLocations.DefaultFile.Substring(($MirrorSQLDefaultLocations.DefaultFile.Length) - 1) -ne "\"))
{
$MirrorSQLDefaultLocations.DefaultFile + "\"
}
Else
{
$MirrorSQLDefaultLocations.DefaultFile
}
##Log File
$MirrorSQLDefaultLocations.DefaultLog =
If (($MirrorSQLDefaultLocations.DefaultLog.Substring(($MirrorSQLDefaultLocations.DefaultLog.Length) - 1) -ne "\"))
{
$MirrorSQLDefaultLocations.DefaultLog + "\"
}
Else
{
$MirrorSQLDefaultLocations.DefaultLog
}
##Backup File
$MirrorSQLDefaultLocations.BackupDirectory =
If (($MirrorSQLDefaultLocations.BackupDirectory.Substring(($MirrorSQLDefaultLocations.BackupDirectory.Length) - 1) -ne "\"))
{
$MirrorSQLDefaultLocations.BackupDirectory + "\"
}
Else
{
$MirrorSQLDefaultLocations.BackupDirectory
}
#This Is for the Error Box That Will Display for All Other Compatiblity Concerns
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
$Form4 = New-Object System.Windows.Forms.Form
$Form4.Text = "There Is An Error With One Or More Properties"
$Form4.Size = New-Object System.Drawing.Size(375,300)
$Form4.ControlBox = $False
$Form4.StartPosition = "CenterScreen"
$OKButton4 = New-Object System.Windows.Forms.Button
$OKButton4.Location = New-Object System.Drawing.Size(108,240)
$OKButton4.Size = New-Object System.Drawing.Size(80,25)
$OKButton4.Text = "OK"
$OKButton4.Add_Click(
{
##Re-Runs These Variables to Make Sure The Necessary Changes Were Made
##Principal
$PrincipalPort = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort
$PrincipalTcpEnabled = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled
$PrincipalSQLAccess = ($Databases.Logins.Name -like '*\Administrators').Length
##Mirror
$MirrorPort = Invoke-Command -Session $MirrorSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort}
$MirrorTcpEnabled = Invoke-Command -Session $MirrorSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled}
$MirrorSQLAccess = Invoke-Command -Session $MirrorSession -ScriptBlock {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$MirrorDatabases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
($MirrorDatabases.Logins.Name -like '*\Administrators').Length
}
##Witness
$WitnessPort = Invoke-Command -Session $WitnessSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp' | Where PSChildName -eq 'IPAll' | Get-ItemProperty | Select-Object -ExpandProperty TcpPort}
$WitnessTcpEnabled = Invoke-Command -Session $WitnessSession -ScriptBlock {Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib' | Where PSChildName -eq 'Tcp' | Get-ItemProperty | Select-Object -ExpandProperty Enabled}
$WitnessSQLAccess = Invoke-Command -Session $WitnessSession -ScriptBlock {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$Databases = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "Localhost"
($Databases.Logins.Name -like '*\Administrators').Length
}
##Need A Variable Here To Populate Error Message Box
$ErrorOutput = "You Must Correct ALL Errors to Proceed. These Issues Still Need To Be Resolved:`n"
If (($PrincipalPort -eq $MirrorPort -and $PrincipalPort -eq $WitnessPort) -eq $False)
{
$ErrorOutput += "`nListening Port"
}
If (($PrincipalTcpEnabled -eq 1 -and $MirrorTcpEnabled -eq 1 -and $WitnessTcpEnabled -eq 1) -eq $False)
{
$ErrorOutput += "`nTCP Enabled"
}
If (($PrincipalSQLAccess + $MirrorSQLAccess +$WitnessSQLAccess -gt 2) -eq $False)
{
$ErrorOutput += "`nAdministrators Group Access"
}
##Checks To See If Changes Were Made; If Not, Message Box Will Be Triggered; Else, The Window Will Close And Script Will Continue
If ((($PrincipalPort -eq $MirrorPort -and $PrincipalPort -eq $WitnessPort) -and ($PrincipalTcpEnabled -eq 1 -and $MirrorTcpEnabled -eq 1 -and $WitnessTcpEnabled -eq 1) -and ($PrincipalSQLAccess + $MirrorSQLAccess +$WitnessSQLAccess -gt 2)) -eq $False)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show($ErrorOutput,"Errors Still Not Resolved","OK","Error")
}
Else
{
$Form4.Close()
}
})
$Form4.Controls.Add($OKButton4)
$CancelButton4 = New-Object System.Windows.Forms.Button
$CancelButton4.Location = New-Object System.Drawing.Size(208,240)
$CancelButton4.Size = New-Object System.Drawing.Size(80,24)
$CancelButton4.Text = "Quit"
$CancelButton4.Add_Click(
{
#Will Quit The Script And Revert Server
Net User SQL_MirrorUser /Delete
Invoke-Command -Session $MirrorSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
Invoke-Command -Session $WitnessSession -ScriptBlock {Net User SQL_MirrorUser /Delete}
$Form4.Close()
[Environment]::Exit(0)
})
$Form4.Controls.Add($CancelButton4)
##Text Box To Display SQL Server Properties
#*****************************************************************************
#*****************************************************************************
$ObjTextBoxSQLProperties = New-Object System.Windows.Forms.Label
$ObjTextBoxSQLProperties.Location = New-Object System.Drawing.Size(22,32)
$ObjTextBoxSQLProperties.Size = New-Object System.Drawing.Size(520,240)
$ObjTextBoxSQLProperties.Text =
"SQL Server Properties On Each Server
Principal Server Listening Port: " + $PrincipalPort + "
Mirror Server Listening Port: " + $MirrorPort + "
Witness Server Listening Port: " + $WitnessPort + "
TCP Enabled - Principal: " + ($PrincipalTcpEnabled -as [Bool]) + "
TCP Enabled - Mirror: " + ($MirrorTcpEnabled -as [Bool]) + "
TCP Enabled - Witness: " + ($WitnessTcpEnabled -as [Bool]) + "
Admin Access for SQL - Principal: " + ($PrincipalSQLAccess -as [Bool]) + "
Admin Access for SQL - Mirror: " + ($MirrorSQLAccess -as [Bool]) + "
Admin Access for SQL - Witness: " + ($WitnessSQLAccess -as [Bool])
$Font4 = New-Object System.Drawing.Font("Lucida Console", 8.5)
$ObjTextBoxSQLProperties.Font = $Font4
$Form4.Controls.Add($ObjTextBoxSQLProperties)
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Will Only Call This Form If The SQL Properties Do Not Match
If ((($PrincipalPort -eq $MirrorPort -and $PrincipalPort -eq $WitnessPort) -and ($PrincipalTcpEnabled -eq 1 -and $MirrorTcpEnabled -eq 1 -and $WitnessTcpEnabled -eq 1) -and ($PrincipalSQLAccess + $MirrorSQLAccess +$WitnessSQLAccess -gt 2)) -eq $False)
{
#Calls The Form To Display
#*****************************************************************************
$Form4.Add_Shown(
{
$Form4.Activate()
})
[Void] $Form4.ShowDialog()
}
#Updating Host Files On Servers
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
##Principal Server
$HostEntries = "
",$PrincipalIP," PrincipalServer
",$MirrorIP," MirrorServer
",$WitnessIP," WitnessServer" -join ''
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value $HostEntries
##Mirror Server
Invoke-Command -Session $MirrorSession -ScriptBlock {Param($HostEntries) Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value $HostEntries} -Args $HostEntries
##Witness Server
$HostEntries += "
",$PrincipalIP," ",$PrincipalHostName,"
",$MirrorIP," ",$MirrorHostName -join ''##Yes This Is Necessary; It Took Me Three Weeks To Figure This Out
Invoke-Command -Session $WitnessSession -ScriptBlock {Param($HostEntries) Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value $HostEntries} -Args $HostEntries
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Scripts For SQL Mirroring
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
##Various Variables
$File = 'C:\rs-pkgs\SQLMirrorScripts\Scripts'
$File2 = 'C:\rs-pkgs\SQLMirrorScripts\Certificates'
$Date = Get-Date
$Length = 18
$NumberOfNonAlphanumericCharacters = 3
$SQLUserpword = [Web.Security.Membership]::GeneratePassword($Length,$NumberOfNonAlphanumericCharacters)
$SQLCertpword = [Web.Security.Membership]::GeneratePassword($Length,$NumberOfNonAlphanumericCharacters)
##Creating Directory For Scripts And Certs
New-Item -ItemType Directory -Path $File
New-Item -ItemType Directory -Path $File2
#Certificate Scripts
#*****************************************************************************
#*****************************************************************************
##Principal Certificate Script
$SQLPrincipalCert =
'USE MASTER
;
IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys where name = ''##MS_DatabaseMasterKey##'')
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+$SQLCertpword+'''
;
IF NOT EXISTS (select 1 from sys.databases where [is_master_key_encrypted_by_server] = 1)
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY
;
IF NOT EXISTS (SELECT 1 FROM sys.certificates WHERE name = ''PrincipalServerCert'')
CREATE CERTIFICATE PrincipalServerCert
WITH SUBJECT = ''Principal Server Certificate''
,START_DATE =''' + $Date.AddDays(-1).ToShortDateString() + ''''+
',EXPIRY_DATE =''' + $Date.AddYears(20).ToShortDateString() + '''
BACKUP CERTIFICATE PrincipalServerCert TO FILE = '''+ $File2 + '\PrincipalServerCert.crt''' > ($File + '\PrincipalServerCertScript.sql')
##Mirror Certificate Script
$SQLMirrorCert =
'USE MASTER
;
IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys where name = ''##MS_DatabaseMasterKey##'')
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+$SQLCertpword+'''
;
IF NOT EXISTS (select 1 from sys.databases where [is_master_key_encrypted_by_server] = 1)
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY
;
IF NOT EXISTS (SELECT 1 FROM sys.certificates WHERE name = ''MirrorServerCert'')
CREATE CERTIFICATE MirrorServerCert
WITH SUBJECT = ''Mirror Server Certificate''
,START_DATE =''' + $Date.AddDays(-1).ToShortDateString() + ''''+
',EXPIRY_DATE =''' + $Date.AddYears(20).ToShortDateString() + '''
BACKUP CERTIFICATE MirrorServerCert TO FILE = '''+ $File2 + '\MirrorServerCert.crt''' > ($File + '\MirrorServerCertScript.sql')
##Witness Certificate Creation Script
$SQLWitnessCert =
'USE MASTER
;
IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys where name = ''##MS_DatabaseMasterKey##'')
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+$SQLCertpword+'''
;
IF NOT EXISTS (select 1 from sys.databases where [is_master_key_encrypted_by_server] = 1)
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY
;
IF NOT EXISTS (SELECT 1 FROM sys.certificates WHERE name = ''WitnessServerCert'')
CREATE CERTIFICATE WitnessServerCert
WITH SUBJECT = ''Witness Server Certificate''
,START_DATE =''' + $Date.AddDays(-1).ToShortDateString() + ''''+
',EXPIRY_DATE =''' + $Date.AddYears(20).ToShortDateString() + '''
BACKUP CERTIFICATE WitnessServerCert TO FILE = '''+ $File2 + '\WitnessServerCert.crt''' > ($File + '\WitnessServerCertScript.sql')
#Scripts For End-Points
#*****************************************************************************
#*****************************************************************************
##Principal End-Point Script
$SQLPrincipalEndPoint =
'USE Master
;
--Check If Mirroring Endpoint Exists
IF NOT EXISTS(SELECT * FROM sys.endpoints WHERE type = 4)
CREATE ENDPOINT DBMirrorEndPoint
STATE = STARTED AS TCP (LISTENER_PORT = 4040)
FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE PrincipalServerCert, ENCRYPTION = REQUIRED, ROLE = ALL )' > ($File + '\PrincipalServerEndPointScript.sql')
##Mirror End-Point Script
$SQLMirrorEndPoint =
'USE Master
;
--Check If Mirroring Endpoint Exists
IF NOT EXISTS(SELECT * FROM sys.endpoints WHERE type = 4)
CREATE ENDPOINT DBMirrorEndPoint
STATE=STARTED AS TCP (LISTENER_PORT = 4041)
FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE MirrorServerCert, ENCRYPTION = REQUIRED, ROLE = ALL )' > ($File + '\MirrorServerEndPointScript.sql')
##Witness End-Point Script
$SQLWitnessEndPoint =
'USE Master
;
--Check If Mirroring Endpoint Exists
IF NOT EXISTS(SELECT * FROM sys.endpoints WHERE type = 4)
CREATE ENDPOINT DBMirrorEndPoint
STATE=STARTED AS TCP (LISTENER_PORT = 4042)
FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE WitnessServerCert, ENCRYPTION = REQUIRED, ROLE = WITNESS )' > ($File + '\WitnessServerEndPointScript.sql')
#Scripts For Login And Grants
#*****************************************************************************
#*****************************************************************************
##Principal Login And Grant Script
$SQLPrincipalLoginGrant =
'USE MASTER
;
--For Mirror Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''MirrorServerUser'')
CREATE LOGIN MirrorServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''MirrorServerUser'')
CREATE USER MirrorServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''MirrorDBCertPub'')
CREATE CERTIFICATE MirrorDBCertPub AUTHORIZATION MirrorServerUser
FROM FILE = ''' + $File2 + '\MirrorServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO MirrorServerUser
;
--For Witness Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''WitnessServerUser'')
CREATE LOGIN WitnessServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''WitnessServerUser'')
CREATE USER WitnessServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''WitnessDBCertPub'')
CREATE CERTIFICATE WitnessDBCertPub AUTHORIZATION WitnessServerUser
FROM FILE = ''' + $File2 + '\WitnessServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO WitnessServerUser
;' > ($File + '\PrincipalServerLoginGrantScript.sql')
##Mirror Login And Grant Script
$SQLMirrorLoginGrant =
'USE MASTER
;
--For Principal Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''PrincipalServerUser'')
CREATE LOGIN PrincipalServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''PrincipalServerUser'')
CREATE USER PrincipalServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''PrincipalDBCertPub'')
CREATE CERTIFICATE PrincipalDBCertPub AUTHORIZATION PrincipalServerUser
FROM FILE = ''' + $File2 + '\PrincipalServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO PrincipalServerUser
;
--For Witness Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''WitnessServerUser'')
CREATE LOGIN WitnessServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''WitnessServerUser'')
CREATE USER WitnessServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''WitnessDBCertPub'')
CREATE CERTIFICATE WitnessDBCertPub AUTHORIZATION WitnessServerUser
FROM FILE = ''' + $File2 + '\WitnessServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO WitnessServerUser
;' > ($File + '\MirrorServerLoginGrantScript.sql')
#Witness Login And Grant Script
$SQLWitnessLoginGrant =
'USE MASTER
GO
--For Principal Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''PrincipalServerUser'')
CREATE LOGIN PrincipalServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''PrincipalServerUser'')
CREATE USER PrincipalServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''PrincipalDBCertPub'')
CREATE CERTIFICATE PrincipalDBCertPub AUTHORIZATION PrincipalServerUser
FROM FILE = ''' + $File2 + '\PrincipalServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO PrincipalServerUser
GO
--For Mirror Server To Connect
IF NOT EXISTS(SELECT 1 FROM sys.syslogins WHERE name = ''MirrorServerUser'')
CREATE LOGIN MirrorServerUser WITH PASSWORD = ''' + $SQLUserpword + '''
IF NOT EXISTS(SELECT 1 FROM sys.sysusers WHERE name = ''MirrorServerUser'')
CREATE USER MirrorServerUser;
IF NOT EXISTS(SELECT 1 FROM sys.certificates WHERE name = ''MirrorDBCertPub'')
CREATE CERTIFICATE MirrorDBCertPub AUTHORIZATION MirrorServerUser
FROM FILE = ''' + $File2 + '\MirrorServerCert.crt''
GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO MirrorServerUser
GO' > ($File + '\WitnessServerLoginGrantScript.sql')
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
#Concatinating SQL Scipts To Run Invoke Commands
#*****************************************************************************
##Certificate Scripts
$PrincipalCertScript = $File + "\PrincipalServerCertScript.sql"
$MirrorCertScript = $File + "\MirrorServerCertScript.sql"
$WitnessCertScript = $File + "\WitnessServerCertScript.sql"
##End-Point Scripts
$PrincipalEndPointScript = $File + "\PrincipalServerEndPointScript.sql"
$MirrorEndPointScript = $File + "\MirrorServerEndPointScript.sql"
$WitnessEndPointScript = $File + "\WitnessServerEndPointScript.sql"
##Login And Grant Scripts
$PrincipalLoginGrantScript = $File + "\PrincipalServerLoginGrantScript.sql"
$MirrorLoginGrantScript = $File + "\MirrorServerLoginGrantScript.sql"
$WitnessLoginGrantScript = $File + "\WitnessServerLoginGrantScript.sql"
#Create Certificates And Move Copy To Each Server
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************
##Running SQL Certificate Script For Principal Server
Import-Module SQLPS
Invoke-Sqlcmd -InputFile $PrincipalCertScript
Set-Location C:\
##Running SQL Certificate Script For Mirror Server
New-PSSession $MirrorSession
Invoke-Command -Session $MirrorSession -ScriptBlock {Param($File) New-Item -ItemType Directory -Path $File} -Args $File
Invoke-Command -Session $MirrorSession -ScriptBlock {Param($File2) New-Item -ItemType Directory -Path $File2} -Args $File2
Net Use M: \\MirrorServer\c$ /User:SQL_MirrorUser $ServerUserpword ##Had To Add This Because There Is A Bug With BitsTrasfer That Ignores The Credentials And Copy-Item Does Not Support Credentials - https://connect.microsoft.com/PowerShell/feedback/details/837010/start-bitstransfer-ignores-credential-parameter
Start-BitsTransfer -Source ($File + '\MirrorServerCertScript.sql') -Destination ('\\MirrorServer\' + $File.Substring(0,1) + '$' + $File.Substring(2)) -Credential $RemotingCred
Invoke-Command -Session $MirrorSession -ScriptBlock {Import-Module SQLPS}
Invoke-Command -Session $MirrorSession -ScriptBlock {Param($MirrorCertScript) Invoke-Sqlcmd -InputFile $MirrorCertScript} -Args $MirrorCertScript
#Running SQL Certificate Script For Witness Server
New-PSSession $WitnessSession
Invoke-Command -Session $WitnessSession -ScriptBlock {Param($File) New-Item -ItemType Directory -Path $File} -Args $File
Invoke-Command -Session $WitnessSession -ScriptBlock {Param($File2) New-Item -ItemType Directory -Path $File2} -Args $File2
Net Use W: \\WitnessServer\C$ /User:SQL_MirrorUser $ServerUserpword ##Had To Add This Because There Is A Bug With BitsTrasfer That Ignores The Credentials And Copy-Item Does Not Support Credentials - https://connect.microsoft.com/PowerShell/feedback/details/837010/start-bitstransfer-ignores-credential-parameter
Start-BitsTransfer -Source ($File + '\WitnessServerCertScript.sql') -Destination ('\\WitnessServer\' + $File.Substring(0,1) + '$' + $File.Substring(2)) -Credential $RemotingCred
Invoke-Command -Session $WitnessSession -ScriptBlock {Import-Module SQLPS}
Invoke-Command -Session $WitnessSession -ScriptBlock {Param($WitnessCertScript) Invoke-Sqlcmd -InputFile $WitnessCertScript} -Args $WitnessCertScript
##Copies All Certificates To Each Server
Start-BitsTransfer -Source ($File2 + '\PrincipalServerCert.crt') -Destination ('\\MirrorServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2)) -Credential $RemotingCred
Start-BitsTransfer -Source ($File2 + '\PrincipalServerCert.crt') -Destination ('\\WitnessServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2)) -Credential $RemotingCred
Start-BitsTransfer -Source ('\\MirrorServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2) + '\MirrorServerCert.crt') -Destination $File2 -Credential $RemotingCred
Start-BitsTransfer -Source ('\\MirrorServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2) + '\MirrorServerCert.crt') -Destination ('\\WitnessServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2)) -Credential $RemotingCred
Start-BitsTransfer -Source ('\\WitnessServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2) + '\WitnessServerCert.crt') -Destination $File2 -Credential $RemotingCred
Start-BitsTransfer -Source ('\\WitnessServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2) + '\WitnessServerCert.crt') -Destination ('\\MirrorServer\' + $File2.Substring(0,1) + '$' + $File2.Substring(2)) -Credential $RemotingCred
#*****************************************************************************
#*****************************************************************************
#*****************************************************************************