-
Notifications
You must be signed in to change notification settings - Fork 225
/
MSFT_SqlAGReplica.psm1
709 lines (580 loc) · 28.4 KB
/
MSFT_SqlAGReplica.psm1
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
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDSCHelper.psm1') `
-Force
<#
.SYNOPSIS
Gets the specified Availability Group Replica from the specified Availability Group.
.PARAMETER Name
The name of the availability group replica.
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
Name of the SQL instance to be configued.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String]
$Name,
[Parameter(Mandatory = $true)]
[String]
$AvailabilityGroupName,
[Parameter(Mandatory = $true)]
[String]
$SQLServer,
[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName
)
# Connect to the instance
$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
# Is this node actively hosting the SQL instance?
$isActiveNode = Test-ActiveNode -ServerObject $serverObject
# Get the endpoint properties
$endpoint = $serverObject.Endpoints | Where-Object { $_.EndpointType -eq 'DatabaseMirroring' }
if ( $endpoint )
{
$endpointPort = $endpoint.Protocol.Tcp.ListenerPort
}
# Create the return object
$alwaysOnAvailabilityGroupReplicaResource = @{
Ensure = 'Absent'
Name = ''
AvailabilityGroupName = ''
AvailabilityMode = ''
BackupPriority = ''
ConnectionModeInPrimaryRole = ''
ConnectionModeInSecondaryRole = ''
FailoverMode = ''
EndpointUrl = ''
IsActiveNode = $isActiveNode
ReadOnlyRoutingConnectionUrl = ''
ReadOnlyRoutingList = @()
SQLServer = $SQLServer
SQLInstanceName = $SQLInstanceName
EndpointPort = $endpointPort
SQLServerNetName = $serverObject.NetName
}
# Get the availability group
$availabilityGroup = $serverObject.AvailabilityGroups[$AvailabilityGroupName]
if ( $availabilityGroup )
{
# Add the Availability Group name to the results
$alwaysOnAvailabilityGroupReplicaResource.AvailabilityGroupName = $availabilityGroup.Name
# Try to find the replica
$availabilityGroupReplica = $availabilityGroup.AvailabilityReplicas[$Name]
if ( $availabilityGroupReplica )
{
# Add the Availability Group Replica properties to the results
$alwaysOnAvailabilityGroupReplicaResource.Ensure = 'Present'
$alwaysOnAvailabilityGroupReplicaResource.Name = $availabilityGroupReplica.Name
$alwaysOnAvailabilityGroupReplicaResource.AvailabilityMode = $availabilityGroupReplica.AvailabilityMode
$alwaysOnAvailabilityGroupReplicaResource.BackupPriority = $availabilityGroupReplica.BackupPriority
$alwaysOnAvailabilityGroupReplicaResource.ConnectionModeInPrimaryRole = $availabilityGroupReplica.ConnectionModeInPrimaryRole
$alwaysOnAvailabilityGroupReplicaResource.ConnectionModeInSecondaryRole = $availabilityGroupReplica.ConnectionModeInSecondaryRole
$alwaysOnAvailabilityGroupReplicaResource.FailoverMode = $availabilityGroupReplica.FailoverMode
$alwaysOnAvailabilityGroupReplicaResource.EndpointUrl = $availabilityGroupReplica.EndpointUrl
$alwaysOnAvailabilityGroupReplicaResource.ReadOnlyRoutingConnectionUrl = $availabilityGroupReplica.ReadOnlyRoutingConnectionUrl
$alwaysOnAvailabilityGroupReplicaResource.ReadOnlyRoutingList = $availabilityGroupReplica.ReadOnlyRoutingList
}
}
return $alwaysOnAvailabilityGroupReplicaResource
}
<#
.SYNOPSIS
Creates or removes the availability group replica in accordance with the desired state.
.PARAMETER Name
The name of the availability group replica.
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
Name of the SQL instance to be configued.
.PARAMETER PrimaryReplicaSQLServer
Hostname of the SQL Server where the primary replica is expected to be active. If the primary replica is not found here, the resource will attempt to find the host that holds the primary replica and connect to it.
.PARAMETER PrimaryReplicaSQLInstanceName
Name of the SQL instance where the primary replica lives.
.PARAMETER Ensure
Specifies if the availability group should be present or absent. Default is Present.
.PARAMETER AvailabilityMode
Specifies the replica availability mode. Default is 'AsynchronousCommit'.
.PARAMETER BackupPriority
Specifies the desired priority of the replicas in performing backups. The acceptable values for this parameter are integers from 0 through 100. Of the set of replicas which are online and available, the replica that has the highest priority performs the backup. Default is 50.
.PARAMETER ConnectionModeInPrimaryRole
Specifies how the availability replica handles connections when in the primary role.
.PARAMETER ConnectionModeInSecondaryRole
Specifies how the availability replica handles connections when in the secondary role.
.PARAMETER EndpointHostName
Specifies the hostname or IP address of the availability group replica endpoint. Default is the instance network name which is set in the code because the value can only be determined when connected to the SQL Instance.
.PARAMETER FailoverMode
Specifies the failover mode. Default is Manual.
.PARAMETER ReadOnlyRoutingConnectionUrl
Specifies the fully-qualified domain name (FQDN) and port to use when routing to the replica for read only connections.
.PARAMETER ReadOnlyRoutingList
Specifies an ordered list of replica server names that represent the probe sequence for connection director to use when redirecting read-only connections through this availability replica. This parameter applies if the availability replica is the current primary replica of the availability group.
.PARAMETER ProcessOnlyOnActiveNode
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
Not used in Set-TargetResource.
#>
function Set-TargetResource
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)]
[String]
$Name,
[Parameter(Mandatory = $true)]
[String]
$AvailabilityGroupName,
[Parameter(Mandatory = $true)]
[String]
$SQLServer,
[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName,
[Parameter()]
[String]
$PrimaryReplicaSQLServer,
[Parameter()]
[String]
$PrimaryReplicaSQLInstanceName,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure = 'Present',
[Parameter()]
[ValidateSet('AsynchronousCommit', 'SynchronousCommit')]
[String]
$AvailabilityMode = 'AsynchronousCommit',
[Parameter()]
[ValidateRange(0, 100)]
[UInt32]
$BackupPriority = 50,
[Parameter()]
[ValidateSet('AllowAllConnections', 'AllowReadWriteConnections')]
[String]
$ConnectionModeInPrimaryRole,
[Parameter()]
[ValidateSet('AllowNoConnections', 'AllowReadIntentConnectionsOnly', 'AllowAllConnections')]
[String]
$ConnectionModeInSecondaryRole,
[Parameter()]
[String]
$EndpointHostName,
[Parameter()]
[ValidateSet('Automatic', 'Manual')]
[String]
$FailoverMode = 'Manual',
[Parameter()]
[String]
$ReadOnlyRoutingConnectionUrl,
[Parameter()]
[String[]]
$ReadOnlyRoutingList,
[Parameter()]
[Boolean]
$ProcessOnlyOnActiveNode
)
Import-SQLPSModule
# Connect to the instance
$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
# Determine if HADR is enabled on the instance. If not, throw an error
if ( -not $serverObject.IsHadrEnabled )
{
throw New-TerminatingError -ErrorType HadrNotEnabled -FormatArgs $Ensure, $SQLInstanceName -ErrorCategory NotImplemented
}
# Get the Availability Group if it exists
$availabilityGroup = $serverObject.AvailabilityGroups[$AvailabilityGroupName]
# Make sure we're communicating with the primary replica in order to make changes to the replica
if ( $availabilityGroup )
{
while ( $availabilityGroup.LocalReplicaRole -ne 'Primary' )
{
$primaryServerObject = Get-PrimaryReplicaServerObject -ServerObject $serverObject -AvailabilityGroup $availabilityGroup
$availabilityGroup = $primaryServerObject.AvailabilityGroups[$AvailabilityGroupName]
}
}
switch ( $Ensure )
{
Absent
{
if ( $availabilityGroup )
{
$availabilityGroupReplica = $availabilityGroup.AvailabilityReplicas[$Name]
if ( $availabilityGroupReplica )
{
try
{
Remove-SqlAvailabilityReplica -InputObject $availabilityGroupReplica -Confirm:$false -ErrorAction Stop
}
catch
{
throw New-TerminatingError -ErrorType RemoveAvailabilityGroupReplicaFailed -FormatArgs $Name -ErrorCategory ResourceUnavailable -InnerException $_.Exception
}
}
}
}
Present
{
# Ensure the appropriate cluster permissions are present
Test-ClusterPermissions -ServerObject $serverObject
# Make sure a database mirroring endpoint exists.
$endpoint = $serverObject.Endpoints | Where-Object { $_.EndpointType -eq 'DatabaseMirroring' }
if ( -not $endpoint )
{
throw New-TerminatingError -ErrorType DatabaseMirroringEndpointNotFound -FormatArgs $SQLServer, $SQLInstanceName -ErrorCategory ObjectNotFound
}
# If a hostname for the endpoint was not specified, define it now.
if ( -not $EndpointHostName )
{
$EndpointHostName = $serverObject.NetName
}
# Get the endpoint port
$endpointPort = $endpoint.Protocol.Tcp.ListenerPort
# Determine if the Availability Group exists on the instance
if ( $availabilityGroup )
{
# Make sure the replia exists on the instance. If the availability group exists, the replica should exist.
$availabilityGroupReplica = $availabilityGroup.AvailabilityReplicas[$Name]
if ( $availabilityGroupReplica )
{
if ( $AvailabilityMode -ne $availabilityGroupReplica.AvailabilityMode )
{
$availabilityGroupReplica.AvailabilityMode = $AvailabilityMode
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $BackupPriority -ne $availabilityGroupReplica.BackupPriority )
{
$availabilityGroupReplica.BackupPriority = $BackupPriority
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
# Make sure ConnectionModeInPrimaryRole has a value in order to avoid false positive matches when the parameter is not defined
if ( ( -not [string]::IsNullOrEmpty($ConnectionModeInPrimaryRole) ) -and ( $ConnectionModeInPrimaryRole -ne $availabilityGroupReplica.ConnectionModeInPrimaryRole ) )
{
$availabilityGroupReplica.ConnectionModeInPrimaryRole = $ConnectionModeInPrimaryRole
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
# Make sure ConnectionModeInSecondaryRole has a value in order to avoid false positive matches when the parameter is not defined
if ( ( -not [string]::IsNullOrEmpty($ConnectionModeInSecondaryRole) ) -and ( $ConnectionModeInSecondaryRole -ne $availabilityGroupReplica.ConnectionModeInSecondaryRole ) )
{
$availabilityGroupReplica.ConnectionModeInSecondaryRole = $ConnectionModeInSecondaryRole
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
# Break out the EndpointUrl properties
$currentEndpointProtocol, $currentEndpointHostName, $currentEndpointPort = $availabilityGroupReplica.EndpointUrl.Replace('//', '').Split(':')
if ( $endpoint.Protocol.Tcp.ListenerPort -ne $currentEndpointPort )
{
$newEndpointUrl = $availabilityGroupReplica.EndpointUrl.Replace($currentEndpointPort, $endpoint.Protocol.Tcp.ListenerPort)
$availabilityGroupReplica.EndpointUrl = $newEndpointUrl
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $EndpointHostName -ne $currentEndpointHostName )
{
$newEndpointUrl = $availabilityGroupReplica.EndpointUrl.Replace($currentEndpointHostName, $EndpointHostName)
$availabilityGroupReplica.EndpointUrl = $newEndpointUrl
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $currentEndpointProtocol -ne 'TCP' )
{
$newEndpointUrl = $availabilityGroupReplica.EndpointUrl.Replace($currentEndpointProtocol, 'TCP')
$availabilityGroupReplica.EndpointUrl = $newEndpointUrl
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $FailoverMode -ne $availabilityGroupReplica.FailoverMode )
{
$availabilityGroupReplica.FailoverMode = $FailoverMode
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $ReadOnlyRoutingConnectionUrl -ne $availabilityGroupReplica.ReadOnlyRoutingConnectionUrl )
{
$availabilityGroupReplica.ReadOnlyRoutingConnectionUrl = $ReadOnlyRoutingConnectionUrl
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
if ( $ReadOnlyRoutingList -ne $availabilityGroupReplica.ReadOnlyRoutingList )
{
$availabilityGroupReplica.ReadOnlyRoutingList = $ReadOnlyRoutingList
Update-AvailabilityGroupReplica -AvailabilityGroupReplica $availabilityGroupReplica
}
}
else
{
throw New-TerminatingError -ErrorType ReplicaNotFound -FormatArgs $Name, $SQLInstanceName -ErrorCategory ResourceUnavailable
}
}
else
{
# Connect to the instance that is supposed to house the primary replica
$primaryReplicaServerObject = Connect-SQL -SQLServer $PrimaryReplicaSQLServer -SQLInstanceName $PrimaryReplicaSQLInstanceName
# Verify the Availability Group exists on the supplied primary replica
$primaryReplicaAvailabilityGroup = $primaryReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName]
if ( $primaryReplicaAvailabilityGroup )
{
# Make sure the instance defined as the primary replica in the parameters is actually the primary replica
$primaryReplicaServerObject = Get-PrimaryReplicaServerObject -ServerObject $primaryReplicaServerObject -AvailabilityGroup $primaryReplicaAvailabilityGroup
$availabilityGroup = $primaryReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName]
# Build the endpoint URL
$endpointUrl = "TCP://$($EndpointHostName):$($endpointPort)"
$newAvailabilityGroupReplicaParams = @{
Name = $Name
InputObject = $primaryReplicaAvailabilityGroup
AvailabilityMode = $AvailabilityMode
EndpointUrl = $endpointUrl
FailoverMode = $FailoverMode
Verbose = $false
}
if ( $BackupPriority )
{
$newAvailabilityGroupReplicaParams.Add('BackupPriority', $BackupPriority)
}
if ( $ConnectionModeInPrimaryRole )
{
$newAvailabilityGroupReplicaParams.Add('ConnectionModeInPrimaryRole', $ConnectionModeInPrimaryRole)
}
if ( $ConnectionModeInSecondaryRole )
{
$newAvailabilityGroupReplicaParams.Add('ConnectionModeInSecondaryRole', $ConnectionModeInSecondaryRole)
}
if ( $ReadOnlyRoutingConnectionUrl )
{
$newAvailabilityGroupReplicaParams.Add('ReadOnlyRoutingConnectionUrl', $ReadOnlyRoutingConnectionUrl)
}
if ( $ReadOnlyRoutingList )
{
$newAvailabilityGroupReplicaParams.Add('ReadOnlyRoutingList', $ReadOnlyRoutingList)
}
# Create the Availability Group Replica
try
{
$availabilityGroupReplica = New-SqlAvailabilityReplica @newAvailabilityGroupReplicaParams
}
catch
{
throw New-TerminatingError -ErrorType CreateAvailabilityGroupReplicaFailed -FormatArgs $Name, $SQLInstanceName -ErrorCategory OperationStopped -InnerException $_.Exception
}
# Join the Availability Group Replica to the Availability Group
try
{
$joinAvailabilityGroupResults = Join-SqlAvailabilityGroup -Name $AvailabilityGroupName -InputObject $serverObject
}
catch
{
throw New-TerminatingError -ErrorType JoinAvailabilityGroupFailed -FormatArgs $Name -ErrorCategory OperationStopped -InnerException $_.Exception
}
}
# The Availability Group doesn't exist on the primary replica
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupNotFound -FormatArgs $AvailabilityGroupName, $PrimaryReplicaSQLInstanceName -ErrorCategory ResourceUnavailable
}
}
}
}
}
<#
.SYNOPSIS
Determines if the availability group replica is in the desired state.
.PARAMETER Name
The name of the availability group replica.
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
Name of the SQL instance to be configued.
.PARAMETER PrimaryReplicaSQLServer
Hostname of the SQL Server where the primary replica is expected to be active. If the primary replica is not found here, the resource will attempt to find the host that holds the primary replica and connect to it.
.PARAMETER PrimaryReplicaSQLInstanceName
Name of the SQL instance where the primary replica lives.
.PARAMETER Ensure
Specifies if the availability group should be present or absent. Default is Present.
.PARAMETER AvailabilityMode
Specifies the replica availability mode. Default is 'AsynchronousCommit'.
.PARAMETER BackupPriority
Specifies the desired priority of the replicas in performing backups. The acceptable values for this parameter are integers from 0 through 100. Of the set of replicas which are online and available, the replica that has the highest priority performs the backup. Default is 50.
.PARAMETER ConnectionModeInPrimaryRole
Specifies how the availability replica handles connections when in the primary role.
.PARAMETER ConnectionModeInSecondaryRole
Specifies how the availability replica handles connections when in the secondary role.
.PARAMETER EndpointHostName
Specifies the hostname or IP address of the availability group replica endpoint. Default is the instance network name which is set in the code because the value can only be determined when connected to the SQL Instance.
.PARAMETER FailoverMode
Specifies the failover mode. Default is Manual.
.PARAMETER ReadOnlyRoutingConnectionUrl
Specifies the fully-qualified domain name (FQDN) and port to use when routing to the replica for read only connections.
.PARAMETER ReadOnlyRoutingList
Specifies an ordered list of replica server names that represent the probe sequence for connection director to use when redirecting read-only connections through this availability replica. This parameter applies if the availability replica is the current primary replica of the availability group.
.PARAMETER ProcessOnlyOnActiveNode
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
Param
(
[Parameter(Mandatory = $true)]
[String]
$Name,
[Parameter(Mandatory = $true)]
[String]
$AvailabilityGroupName,
[Parameter(Mandatory = $true)]
[String]
$SQLServer,
[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName,
[Parameter()]
[String]
$PrimaryReplicaSQLServer,
[Parameter()]
[String]
$PrimaryReplicaSQLInstanceName,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure = 'Present',
[Parameter()]
[ValidateSet('AsynchronousCommit', 'SynchronousCommit')]
[String]
$AvailabilityMode = 'AsynchronousCommit',
[Parameter()]
[ValidateRange(0, 100)]
[UInt32]
$BackupPriority = 50,
[Parameter()]
[ValidateSet('AllowAllConnections', 'AllowReadWriteConnections')]
[String]
$ConnectionModeInPrimaryRole,
[Parameter()]
[ValidateSet('AllowNoConnections', 'AllowReadIntentConnectionsOnly', 'AllowAllConnections')]
[String]
$ConnectionModeInSecondaryRole,
[Parameter()]
[String]
$EndpointHostName,
[Parameter()]
[ValidateSet('Automatic', 'Manual')]
[String]
$FailoverMode = 'Manual',
[Parameter()]
[String]
$ReadOnlyRoutingConnectionUrl,
[Parameter()]
[String[]]
$ReadOnlyRoutingList,
[Parameter()]
[Boolean]
$ProcessOnlyOnActiveNode
)
$getTargetResourceParameters = @{
SQLInstanceName = $SQLInstanceName
SQLServer = $SQLServer
Name = $Name
AvailabilityGroupName = $AvailabilityGroupName
}
# Assume this will pass. We will determine otherwise later
$result = $true
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
<#
If this is supposed to process only the active node, and this is not the
active node, don't bother evaluating the test.
#>
if ( $ProcessOnlyOnActiveNode -and -not $getTargetResourceResult.IsActiveNode )
{
# Use localization if the resource has been converted
New-VerboseMessage -Message ( 'The node "{0}" is not actively hosting the instance "{1}". Exiting the test.' -f $env:COMPUTERNAME,$SQLInstanceName )
return $result
}
switch ($Ensure)
{
'Absent'
{
if ( $getTargetResourceResult.Ensure -eq 'Absent' )
{
$result = $true
}
else
{
$result = $false
}
}
'Present'
{
$parametersToCheck = @(
'Name',
'AvailabilityGroupName',
'SQLServer',
'SQLInstanceName',
'Ensure',
'AvailabilityMode',
'BackupPriority',
'ConnectionModeInPrimaryRole',
'ConnectionModeInSecondaryRole',
'FailoverMode',
'ReadOnlyRoutingConnectionUrl',
'ReadOnlyRoutingList'
)
if ( $getTargetResourceResult.Ensure -eq 'Present' )
{
# PsBoundParameters won't work here because it doesn't account for default values
foreach ( $parameter in $MyInvocation.MyCommand.Parameters.GetEnumerator() )
{
$parameterName = $parameter.Key
$parameterValue = Get-Variable -Name $parameterName -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
# Make sure we don't try to validate a common parameter
if ( $parametersToCheck -contains $parameterName )
{
# If the parameter is Null, a value wasn't provided
if ( -not [string]::IsNullOrEmpty($parameterValue) )
{
if ( $getTargetResourceResult.($parameterName) -ne $parameterValue )
{
New-VerboseMessage -Message "'$($parameterName)' should be '$($parameterValue)' but is '$($getTargetResourceResult.($parameterName))'"
$result = $false
}
}
}
}
# Get the Endpoint URL properties
$currentEndpointProtocol, $currentEndpointHostName, $currentEndpointPort = $getTargetResourceResult.EndpointUrl.Replace('//', '').Split(':')
if ( -not $EndpointHostName )
{
$EndpointHostName = $getTargetResourceResult.SQLServerNetName
}
# Verify the hostname in the endpoint URL is correct
if ( $EndpointHostName -ne $currentEndpointHostName )
{
New-VerboseMessage -Message "'EndpointHostName' should be '$EndpointHostName' but is '$currentEndpointHostName'"
$result = $false
}
# Verify the protocol in the endpoint URL is correct
if ( 'TCP' -ne $currentEndpointProtocol )
{
New-VerboseMessage -Message "'EndpointProtocol' should be 'TCP' but is '$currentEndpointProtocol'"
$result = $false
}
# Verify the port in the endpoint URL is correct
if ( $getTargetResourceResult.EndpointPort -ne $currentEndpointPort )
{
New-VerboseMessage -Message "'EndpointPort' should be '$($getTargetResourceResult.EndpointPort)' but is '$currentEndpointPort'"
$result = $false
}
}
else
{
$result = $false
}
}
}
return $result
}
Export-ModuleMember -Function *-TargetResource