forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-DbaAgListener.ps1
193 lines (156 loc) · 8.17 KB
/
Add-DbaAgListener.ps1
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
function Add-DbaAgListener {
<#
.SYNOPSIS
Adds a listener to an availability group on a SQL Server instance.
.DESCRIPTION
Adds a listener to an availability group on a SQL Server instance.
.PARAMETER SqlInstance
The target SQL Server instance or instances. Server version must be SQL Server version 2012 or higher.
.PARAMETER SqlCredential
Login to the SqlInstance instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
.PARAMETER AvailabilityGroup
The Availability Group to which a listener will be bestowed upon.
.PARAMETER Name
The name of the listener. If one is not specified, the Availability Group name will be used.
Note that Name cannot be used with Multiple Ags.
.PARAMETER IPAddress
Sets the IP address(es) of the availability group listener.
.PARAMETER SubnetIP
Sets the Subnet IP address(es) of the availability group listener.
.PARAMETER SubnetMask
Sets the subnet IP mask(s) of the availability group listener. Defaults to 255.255.255.0.
.PARAMETER Port
Sets the port number used to communicate with the availability group. Defaults to 1433.
.PARAMETER Dhcp
Indicates whether the listener uses DHCP.
.PARAMETER Passthru
Don't create the listener, just pass thru an object that can be further customized before creation.
.PARAMETER InputObject
Enables piping from Get-DbaAvailabilityGroup
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: AvailabilityGroup, HA, AG
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Add-DbaAgListener
.EXAMPLE
PS C:\> Add-DbaAgListener -SqlInstance sql2017 -AvailabilityGroup SharePoint -IPAddress 10.0.20.20
Creates a listener on 10.0.20.20 port 1433 for the SharePoint availability group on sql2017.
.EXAMPLE
PS C:\> Get-DbaAvailabilityGroup -SqlInstance sql2017 -AvailabilityGroup availabilitygroup1 | Add-DbaAgListener -Dhcp
Creates a listener on port 1433 with a dynamic IP for the group1 availability group on sql2017.
.EXAMPLE
PS C:\> Add-DbaAgListener -SqlInstance sql2017 -AvailabilityGroup SharePoint -IPAddress 10.0.20.20,10.1.77.77 -SubnetMask 255.255.252.0
Creates a multi-subnet listener with 10.0.20.20 and 10.1.77.77, on two /22 subnets, on port 1433 for the SharePoint availability group on sql2017.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string[]]$AvailabilityGroup,
[string]$Name,
[ipaddress[]]$IPAddress,
[ipaddress[]]$SubnetIP,
[ipaddress[]]$SubnetMask = "255.255.255.0",
[int]$Port = 1433,
[switch]$Dhcp,
[switch]$Passthru,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.AvailabilityGroup[]]$InputObject,
[switch]$EnableException
)
process {
if (Test-Bound -Not SqlInstance, InputObject) {
Stop-Function -Message "You must supply either -SqlInstance or an Input Object"
return
}
if ((Test-Bound -ParameterName SqlInstance) -and (Test-Bound -Not -ParameterName AvailabilityGroup)) {
Stop-Function -Message "You must specify one or more databases and one or more Availability Groups when using the SqlInstance parameter."
return
}
if ($Dhcp) {
if (Test-Bound -ParameterName IPAddress) {
Stop-Function -Message "You cannot specify both an IP address and the Dhcp switch."
return
}
if ($SubnetMask.Count -gt 1 -or $SubnetIP.Count -gt 1) {
Stop-Function -Message "You can only specify a single subnet when using Dhcp."
return
}
}
if ($SqlInstance) {
$InputObject += Get-DbaAvailabilityGroup -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AvailabilityGroup $AvailabilityGroup
}
if (Test-Bound -ParameterName IPAddress) {
if ($IPAddress.Count -ne $SubnetMask.Count) {
if ($SubnetMask.Count -eq 1) {
# If one subnet mask is supplied, let's assume we want to use the same one for every IP
$SubnetMask = $SubnetMask * $IPAddress.Count
} else {
Stop-Function -Message "When specifying multiple IP addresses, the number of subnet masks must match, or give one mask to be used for all IP addresses."
return
}
}
if (Test-Bound -Not -ParameterName SubnetIP) {
# No subnets (subnet IPs) were supplied but we can calculate them with the netmask
$SubnetIP = for ($ipIndex = 0 ; $ipIndex -lt $IPAddress.Count ; $ipIndex++) {
($IPAddress[$ipIndex].Address -band $SubnetMask[$ipIndex].Address) -as [ipaddress]
}
} else {
if ($IPAddress.Count -ne $SubnetIP.Count) {
if ($SubnetIP.Count -eq 1) {
# If one subnet IP is supplied, let's assume we want to use the same subnet for every IP
$SubnetIP = $SubnetIP * $IPAddress.Count
} else {
Stop-Function -Message "When specifying subnet IPs explicitly, the number of subnets must match the number of IPs, or use one subnet to be applied to all IPs."
return
}
}
}
}
foreach ($ag in $InputObject) {
if ((Test-Bound -Not -ParameterName Name)) {
$Name = $ag.Name
}
if ($Pscmdlet.ShouldProcess($ag.Parent.Name, "Adding $($IPAddress.IPAddressToString) to $($ag.Name)")) {
try {
$aglistener = New-Object Microsoft.SqlServer.Management.Smo.AvailabilityGroupListener -ArgumentList $ag, $Name
$aglistener.PortNumber = $Port
$ipIndex = 0
do {
# add the IPs
$listenerip = New-Object Microsoft.SqlServer.Management.Smo.AvailabilityGroupListenerIPAddress -ArgumentList $aglistener
if (Test-Bound -ParameterName IPAddress) {
$listenerip.IPAddress = $IPAddress[$ipIndex]
}
if ($SubnetIP) {
$listenerip.SubnetMask = $SubnetMask[$ipIndex]
$listenerip.SubnetIP = $SubnetIP[$ipIndex]
}
$listenerip.IsDHCP = $Dhcp
$aglistener.AvailabilityGroupListenerIPAddresses.Add($listenerip)
} while ((++$ipIndex) -lt $IPAddress.Count)
if ($Passthru) {
return $aglistener
} else {
# something is up with .net create(), force a stop
Invoke-Create -Object $aglistener
}
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_
}
Get-DbaAgListener -SqlInstance $ag.Parent -AvailabilityGroup $ag.Name -Listener $Name
}
}
}
}