-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathMSFT_SqlServerEndpoint.psm1
315 lines (265 loc) · 10.1 KB
/
MSFT_SqlServerEndpoint.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
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDscHelper.psm1') `
-Force
<#
.SYNOPSIS
Returns the current state of the endpoint.
.PARAMETER EndpointName
The name of the endpoint.
.PARAMETER ServerName
The host name of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
.PARAMETER InstanceName
The name of the SQL instance to be configured.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$EndpointName,
[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName
)
$getTargetResourceReturnValues = @{
ServerName = $ServerName
InstanceName = $InstanceName
Ensure = 'Absent'
EndpointName = ''
Port = ''
IpAddress = ''
Owner = ''
}
$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message ('Connected to {0}\{1}' -f $ServerName, $InstanceName)
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject.Name -eq $EndpointName)
{
if ($sqlServerObject.Endpoints[$EndPointName].EndpointType -ne 'DatabaseMirroring')
{
throw New-TerminatingError -ErrorType EndpointFoundButWrongType `
-FormatArgs @($EndpointName) `
-ErrorCategory InvalidOperation
}
$getTargetResourceReturnValues.Ensure = 'Present'
$getTargetResourceReturnValues.EndpointName = $endpointObject.Name
$getTargetResourceReturnValues.Port = $endpointObject.Protocol.Tcp.ListenerPort
$getTargetResourceReturnValues.IpAddress = $endpointObject.Protocol.Tcp.ListenerIPAddress
$getTargetResourceReturnValues.Owner = $endpointObject.Owner
}
else
{
$getTargetResourceReturnValues.Ensure = 'Absent'
$getTargetResourceReturnValues.EndpointName = ''
$getTargetResourceReturnValues.Port = ''
$getTargetResourceReturnValues.IpAddress = ''
$getTargetResourceReturnValues.Owner = ''
}
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
}
return $getTargetResourceReturnValues
}
<#
.SYNOPSIS
Create, changes or drops an endpoint.
.PARAMETER EndpointName
The name of the endpoint.
.PARAMETER Ensure
If the endpoint should be present or absent. Default values is 'Present'.
.PARAMETER Port
The network port the endpoint is listening on. Default value is 5022.
.PARAMETER ServerName
The host name of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
.PARAMETER InstanceName
The name of the SQL instance to be configured.
.PARAMETER IpAddress
The network IP address the endpoint is listening on. Defaults to '0.0.0.0' which means listen on any valid IP address.
.PARAMETER Owner
The owner of the endpoint. Default is the login used for the creation.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$EndpointName,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[System.UInt16]
$Port = 5022,
[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,
[Parameter()]
[System.String]
$IpAddress = '0.0.0.0',
[Parameter()]
[System.String]
$Owner
)
$getTargetResourceResult = Get-TargetResource -EndpointName $EndpointName -ServerName $ServerName -InstanceName $InstanceName
$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
if ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Absent')
{
Write-Verbose -Message ('Creating endpoint {0}.' -f $EndpointName)
$endpointObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Endpoint -ArgumentList $sqlServerObject, $EndpointName
$endpointObject.EndpointType = [Microsoft.SqlServer.Management.Smo.EndpointType]::DatabaseMirroring
$endpointObject.ProtocolType = [Microsoft.SqlServer.Management.Smo.ProtocolType]::Tcp
$endpointObject.Protocol.Tcp.ListenerPort = $Port
$endpointObject.Protocol.Tcp.ListenerIPAddress = $IpAddress
if ($PSBoundParameters.ContainsKey('Owner'))
{
$endpointObject.Owner = $Owner
}
$endpointObject.Payload.DatabaseMirroring.ServerMirroringRole = [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]::All
$endpointObject.Payload.DatabaseMirroring.EndpointEncryption = [Microsoft.SqlServer.Management.Smo.EndpointEncryption]::Required
$endpointObject.Payload.DatabaseMirroring.EndpointEncryptionAlgorithm = [Microsoft.SqlServer.Management.Smo.EndpointEncryptionAlgorithm]::Aes
$endpointObject.Create()
$endpointObject.Start()
}
elseif ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Present')
{
# The endpoint already exist, verifying supported endpoint properties so they are in desired state.
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
if ($endpointObject.Protocol.Tcp.ListenerIPAddress -ne $IpAddress)
{
Write-Verbose -Message ('Updating endpoint {0} IP address to {1}.' -f $EndpointName, $IpAddress)
$endpointObject.Protocol.Tcp.ListenerIPAddress = $IpAddress
$endpointObject.Alter()
}
if ($endpointObject.Protocol.Tcp.ListenerPort -ne $Port)
{
Write-Verbose -Message ('Updating endpoint {0} port to {1}.' -f $EndpointName, $Port)
$endpointObject.Protocol.Tcp.ListenerPort = $Port
$endpointObject.Alter()
}
if ($endpointObject.Owner -ne $Owner)
{
Write-Verbose -Message ('Updating endpoint {0} Owner to {1}.' -f $EndpointName, $Owner)
$endpointObject.Owner = $Owner
$endpointObject.Alter()
}
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
}
}
elseif ($Ensure -eq 'Absent' -and $getTargetResourceResult.Ensure -eq 'Present')
{
Write-Verbose -Message ('Dropping endpoint {0}.' -f $EndpointName)
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
$endpointObject.Drop()
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
}
}
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
}
}
<#
.SYNOPSIS
Tests if the principal (login) has the desired permissions.
.PARAMETER EndpointName
The name of the endpoint.
.PARAMETER Ensure
If the endpoint should be present or absent. Default values is 'Present'.
.PARAMETER Port
The network port the endpoint is listening on. Default value is 5022.
.PARAMETER ServerName
The host name of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
.PARAMETER InstanceName
The name of the SQL instance to be configured.
.PARAMETER IpAddress
The network IP address the endpoint is listening on. Defaults to '0.0.0.0' which means listen on any valid IP address.
.PARAMETER Owner
The owner of the endpoint. Default is the login used for the creation.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$EndpointName,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[System.UInt16]
$Port = 5022,
[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,
[Parameter()]
[System.String]
$IpAddress = '0.0.0.0',
[Parameter()]
[System.String]
$Owner
)
$getTargetResourceResult = Get-TargetResource -EndpointName $EndpointName -ServerName $ServerName -InstanceName $InstanceName
if ($getTargetResourceResult.Ensure -eq $Ensure)
{
$result = $true
if ($getTargetResourceResult.Ensure -eq 'Present' `
-and (
$getTargetResourceResult.Port -ne $Port `
-or $getTargetResourceResult.IpAddress -ne $IpAddress
)
)
{
$result = $false
}
elseif ($getTargetResourceResult.Ensure -eq 'Present' -and $Owner)
{
$result = $false
}
}
else
{
$result = $false
}
return $result
}
Export-ModuleMember -Function *-TargetResource