/
Set-DbaNetworkConfiguration.ps1
512 lines (447 loc) 路 27.9 KB
/
Set-DbaNetworkConfiguration.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
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
function Set-DbaNetworkConfiguration {
<#
.SYNOPSIS
Sets the network configuration of a SQL Server instance.
.DESCRIPTION
Sets the network configuration of a SQL Server instance.
Parameters are available for typical tasks like enabling or disabling a protocol or switching between dynamic and static ports.
The object returned by Get-DbaNetworkConfiguration can be used to adjust settings of the properties
and then passed to this command via pipeline or -InputObject parameter.
A change to the network configuration with SQL Server requires a restart to take effect,
support for this can be done via the RestartService parameter.
Remote SQL WMI is used by default, with PS Remoting used as a fallback.
For a detailed explanation of the different properties see the documentation at:
https://docs.microsoft.com/en-us/sql/tools/configuration-manager/sql-server-network-configuration
.PARAMETER SqlInstance
The target SQL Server instance or instances.
.PARAMETER Credential
Credential object used to connect to the Computer as a different user.
.PARAMETER EnableProtocol
Enables one of the following network protocols: SharedMemory, NamedPipes, TcpIp.
.PARAMETER DisableProtocol
Disables one of the following network protocols: SharedMemory, NamedPipes, TcpIp.
.PARAMETER DynamicPortForIPAll
Configures the instance to listen on a dynamic port for all IP addresses.
Will enable the TCP/IP protocol if needed.
Will set TcpIpProperties.ListenAll to $true if needed.
Will reset the last used dynamic port if already set.
.PARAMETER StaticPortForIPAll
Configures the instance to listen on one or more static ports for all IP addresses.
Will enable the TCP/IP protocol if needed.
Will set TcpIpProperties.ListenAll to $true if needed.
.PARAMETER IpAddress
Configures the instance to listen on specific IP addresses only. Listening on all other IP addresses will be disabled.
Takes an array of string with either the IP address (for listening on a dynamic port) or IP address and port seperated by ":".
IPv6 addresses must be enclosed in square brackets, e.g. [2001:db8:4006:812::200e] or [2001:db8:4006:812::200e]:1433 to be able to identify the port.
.PARAMETER RestartService
Every change to the network configuration needs a service restart to take effect.
This switch will force a restart of the service if the network configuration has changed.
.PARAMETER InputObject
The output object from Get-DbaNetworkConfiguration.
Get-DbaNetworkConfiguration has to be run with -OutputType Full (default) to get the complete object.
.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.
.PARAMETER WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
.PARAMETER Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
.NOTES
Tags: Network, Connection, SQLWMI
Author: Andreas Jordan (@JordanOrdix), ordix.de
Website: https://dbatools.io
Copyright: (c) 2021 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Set-DbaNetworkConfiguration
.EXAMPLE
PS C:\> Set-DbaNetworkConfiguration -SqlInstance sql2016 -EnableProtocol SharedMemory -RestartService
Ensures that the shared memory network protocol for the default instance on sql2016 is enabled.
Restarts the service if needed.
.EXAMPLE
PS C:\> Set-DbaNetworkConfiguration -SqlInstance sql2016\test -StaticPortForIPAll 14331, 14332 -RestartService
Ensures that the TCP/IP network protocol is enabled and configured to use the ports 14331 and 14332 for all IP addresses.
Restarts the service if needed.
.EXAMPLE
PS C:\> $netConf = Get-DbaNetworkConfiguration -SqlInstance sqlserver2014a
PS C:\> $netConf.TcpIpProperties.KeepAlive = 60000
PS C:\> $netConf | Set-DbaNetworkConfiguration -RestartService -Confirm:$false
Changes the value of the KeepAlive property for the default instance on sqlserver2014a and restarts the service.
Does not prompt for confirmation.
.EXAMPLE
PS C:\> Set-DbaNetworkConfiguration -SqlInstance sql2016\test -IpAddress 192.168.3.41:1433 -RestartService
Ensures that the TCP/IP network protocol is enabled and configured to only listen on port 1433 of IP address 192.168.3.41.
Restarts the service if needed.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "High", DefaultParameterSetName = 'NonPipeline')]
param (
[Parameter(ParameterSetName = 'NonPipeline', Mandatory = $true, Position = 0)]
[DbaInstanceParameter[]]$SqlInstance,
[Parameter(ParameterSetName = 'NonPipeline')][Parameter(ParameterSetName = 'Pipeline')]
[PSCredential]$Credential,
[Parameter(ParameterSetName = 'NonPipeline')]
[ValidateSet('SharedMemory', 'NamedPipes', 'TcpIp')]
[string]$EnableProtocol,
[Parameter(ParameterSetName = 'NonPipeline')]
[ValidateSet('SharedMemory', 'NamedPipes', 'TcpIp')]
[string]$DisableProtocol,
[Parameter(ParameterSetName = 'NonPipeline')]
[switch]$DynamicPortForIPAll,
[Parameter(ParameterSetName = 'NonPipeline')]
[int[]]$StaticPortForIPAll,
[Parameter(ParameterSetName = 'NonPipeline')]
[string[]]$IpAddress,
[Parameter(ParameterSetName = 'NonPipeline')][Parameter(ParameterSetName = 'Pipeline')]
[switch]$RestartService,
[parameter(ValueFromPipeline, ParameterSetName = 'Pipeline', Mandatory = $true)]
[object[]]$InputObject,
[Parameter(ParameterSetName = 'NonPipeline')][Parameter(ParameterSetName = 'Pipeline')]
[switch]$EnableException
)
begin {
$wmiScriptBlock = {
# This scriptblock will be processed by Invoke-Command2 on the target machine.
# We take on object as the first parameter which has to include the instance name and the target network configuration.
$targetConf = $args[0]
$changes = @()
$verbose = @()
$exception = $null
try {
$verbose += "Starting initialization of WMI object"
# As we go remote, ensure the assembly is loaded
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SqlWmiManagement')
$wmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
$result = $wmi.Initialize()
$verbose += "Initialization of WMI object finished with $result"
# If WMI object is empty, there are no client protocols - so we test for that to see if initialization was successful
$verbose += "Found $($wmi.ServerInstances.Count) instances and $($wmi.ClientProtocols.Count) client protocols inside of WMI object"
$verbose += "Getting server protocols for $($targetConf.InstanceName)"
$wmiServerProtocols = ($wmi.ServerInstances | Where-Object { $_.Name -eq $targetConf.InstanceName } ).ServerProtocols
$verbose += 'Getting server protocol shared memory'
$wmiSpSm = $wmiServerProtocols | Where-Object { $_.Name -eq 'Sm' }
if ($null -eq $targetConf.SharedMemoryEnabled) {
$verbose += 'SharedMemoryEnabled not in target object'
} elseif ($wmiSpSm.IsEnabled -ne $targetConf.SharedMemoryEnabled) {
$wmiSpSm.IsEnabled = $targetConf.SharedMemoryEnabled
$wmiSpSm.Alter()
$changes += "Changed SharedMemoryEnabled to $($targetConf.SharedMemoryEnabled)"
}
$verbose += 'Getting server protocol named pipes'
$wmiSpNp = $wmiServerProtocols | Where-Object { $_.Name -eq 'Np' }
if ($null -eq $targetConf.NamedPipesEnabled) {
$verbose += 'NamedPipesEnabled not in target object'
} elseif ($wmiSpNp.IsEnabled -ne $targetConf.NamedPipesEnabled) {
$wmiSpNp.IsEnabled = $targetConf.NamedPipesEnabled
$wmiSpNp.Alter()
$changes += "Changed NamedPipesEnabled to $($targetConf.NamedPipesEnabled)"
}
$verbose += 'Getting server protocol TCP/IP'
$wmiSpTcp = $wmiServerProtocols | Where-Object { $_.Name -eq 'Tcp' }
if ($null -eq $targetConf.TcpIpEnabled) {
$verbose += 'TcpIpEnabled not in target object'
} elseif ($wmiSpTcp.IsEnabled -ne $targetConf.TcpIpEnabled) {
$wmiSpTcp.IsEnabled = $targetConf.TcpIpEnabled
$wmiSpTcp.Alter()
$changes += "Changed TcpIpEnabled to $($targetConf.TcpIpEnabled)"
}
$verbose += 'Getting properties for server protocol TCP/IP'
$wmiSpTcpEnabled = $wmiSpTcp.ProtocolProperties | Where-Object { $_.Name -eq 'Enabled' }
if ($null -eq $targetConf.TcpIpProperties.Enabled) {
$verbose += 'TcpIpProperties.Enabled not in target object'
} elseif ($wmiSpTcpEnabled.Value -ne $targetConf.TcpIpProperties.Enabled) {
$wmiSpTcpEnabled.Value = $targetConf.TcpIpProperties.Enabled
$wmiSpTcp.Alter()
$changes += "Changed TcpIpProperties.Enabled to $($targetConf.TcpIpProperties.Enabled)"
}
$wmiSpTcpKeepAlive = $wmiSpTcp.ProtocolProperties | Where-Object { $_.Name -eq 'KeepAlive' }
if ($null -eq $targetConf.TcpIpProperties.KeepAlive) {
$verbose += 'TcpIpProperties.KeepAlive not in target object'
} elseif ($wmiSpTcpKeepAlive.Value -ne $targetConf.TcpIpProperties.KeepAlive) {
$wmiSpTcpKeepAlive.Value = $targetConf.TcpIpProperties.KeepAlive
$wmiSpTcp.Alter()
$changes += "Changed TcpIpProperties.KeepAlive to $($targetConf.TcpIpProperties.KeepAlive)"
}
$wmiSpTcpListenOnAllIPs = $wmiSpTcp.ProtocolProperties | Where-Object { $_.Name -eq 'ListenOnAllIPs' }
if ($null -eq $targetConf.TcpIpProperties.ListenAll) {
$verbose += 'TcpIpProperties.ListenAll not in target object'
} elseif ($wmiSpTcpListenOnAllIPs.Value -ne $targetConf.TcpIpProperties.ListenAll) {
$wmiSpTcpListenOnAllIPs.Value = $targetConf.TcpIpProperties.ListenAll
$wmiSpTcp.Alter()
$changes += "Changed TcpIpProperties.ListenAll to $($targetConf.TcpIpProperties.ListenAll)"
}
$verbose += 'Getting properties for IPn'
$wmiIPn = $wmiSpTcp.IPAddresses | Where-Object { $_.Name -ne 'IPAll' }
foreach ($ip in $wmiIPn) {
$ipTarget = $targetConf.TcpIpAddresses | Where-Object { $_.Name -eq $ip.Name }
$ipActive = $ip.IPAddressProperties | Where-Object { $_.Name -eq 'Active' }
if ($null -eq $ipTarget.Active) {
$verbose += 'Active not in target IP address object'
} elseif ($ipActive.Value -ne $ipTarget.Active) {
$ipActive.Value = $ipTarget.Active
$wmiSpTcp.Alter()
$changes += "Changed Active for $($ip.Name) to $($ipTarget.Active)"
}
$ipEnabled = $ip.IPAddressProperties | Where-Object { $_.Name -eq 'Enabled' }
if ($null -eq $ipTarget.Enabled) {
$verbose += 'Enabled not in target IP address object'
} elseif ($ipEnabled.Value -ne $ipTarget.Enabled) {
$ipEnabled.Value = $ipTarget.Enabled
$wmiSpTcp.Alter()
$changes += "Changed Enabled for $($ip.Name) to $($ipTarget.Enabled)"
}
$ipIpAddress = $ip.IPAddressProperties | Where-Object { $_.Name -eq 'IpAddress' }
if ($null -eq $ipTarget.IpAddress) {
$verbose += 'IpAddress not in target IP address object'
} elseif ($ipIpAddress.Value -ne $ipTarget.IpAddress) {
$ipIpAddress.Value = $ipTarget.IpAddress
$wmiSpTcp.Alter()
$changes += "Changed IpAddress for $($ip.Name) to $($ipTarget.IpAddress)"
}
$ipTcpDynamicPorts = $ip.IPAddressProperties | Where-Object { $_.Name -eq 'TcpDynamicPorts' }
if ($null -eq $ipTarget.TcpDynamicPorts) {
$verbose += 'TcpDynamicPorts not in target IP address object'
} elseif ($ipTcpDynamicPorts.Value -ne $ipTarget.TcpDynamicPorts) {
$ipTcpDynamicPorts.Value = $ipTarget.TcpDynamicPorts
$wmiSpTcp.Alter()
$changes += "Changed TcpDynamicPorts for $($ip.Name) to $($ipTarget.TcpDynamicPorts)"
}
$ipTcpPort = $ip.IPAddressProperties | Where-Object { $_.Name -eq 'TcpPort' }
if ($null -eq $ipTarget.TcpPort) {
$verbose += 'TcpPort not in target IP address object'
} elseif ($ipTcpPort.Value -ne $ipTarget.TcpPort) {
$ipTcpPort.Value = $ipTarget.TcpPort
$wmiSpTcp.Alter()
$changes += "Changed TcpPort for $($ip.Name) to $($ipTarget.TcpPort)"
}
}
$verbose += 'Getting properties for IPAll'
$wmiIPAll = $wmiSpTcp.IPAddresses | Where-Object { $_.Name -eq 'IPAll' }
$ipTarget = $targetConf.TcpIpAddresses | Where-Object { $_.Name -eq 'IPAll' }
$ipTcpDynamicPorts = $wmiIPAll.IPAddressProperties | Where-Object { $_.Name -eq 'TcpDynamicPorts' }
if ($null -eq $ipTarget.TcpDynamicPorts) {
$verbose += 'TcpDynamicPorts not in target IP address object'
} elseif ($ipTcpDynamicPorts.Value -ne $ipTarget.TcpDynamicPorts) {
$ipTcpDynamicPorts.Value = $ipTarget.TcpDynamicPorts
$wmiSpTcp.Alter()
$changes += "Changed TcpDynamicPorts for $($wmiIPAll.Name) to $($ipTarget.TcpDynamicPorts)"
}
$ipTcpPort = $wmiIPAll.IPAddressProperties | Where-Object { $_.Name -eq 'TcpPort' }
if ($null -eq $ipTarget.TcpPort) {
$verbose += 'TcpPort not in target IP address object'
} elseif ($ipTcpPort.Value -ne $ipTarget.TcpPort) {
$ipTcpPort.Value = $ipTarget.TcpPort
$wmiSpTcp.Alter()
$changes += "Changed TcpPort for $($wmiIPAll.Name) to $($ipTarget.TcpPort)"
}
} catch {
$exception = $_
}
[PSCustomObject]@{
Changes = $changes
Verbose = $verbose
Exception = $exception
}
}
}
process {
if ($SqlInstance -and (Test-Bound -Not -ParameterName EnableProtocol, DisableProtocol, DynamicPortForIPAll, StaticPortForIPAll, IpAddress)) {
Stop-Function -Message "You must choose an action if SqlInstance is used."
return
}
if ($SqlInstance -and (Test-Bound -ParameterName EnableProtocol, DisableProtocol, DynamicPortForIPAll, StaticPortForIPAll, IpAddress -Not -Max 1)) {
Stop-Function -Message "Only one action is allowed at a time."
return
}
foreach ($instance in $SqlInstance) {
try {
Write-Message -Level Verbose -Message "Get network configuration from $($instance.ComputerName) for instance $($instance.InstanceName)."
$netConf = Get-DbaNetworkConfiguration -SqlInstance $instance -Credential $Credential -EnableException
} catch {
Stop-Function -Message "Failed to collect network configuration from $($instance.ComputerName) for instance $($instance.InstanceName)." -Target $instance -ErrorRecord $_ -Continue
}
if ($EnableProtocol) {
if ($netConf."${EnableProtocol}Enabled") {
Write-Message -Level Verbose -Message "Protocol $EnableProtocol is already enabled on $instance."
} else {
Write-Message -Level Verbose -Message "Will enable protocol $EnableProtocol on $instance."
$netConf."${EnableProtocol}Enabled" = $true
if ($EnableProtocol -eq 'TcpIp') {
$netConf.TcpIpProperties.Enabled = $true
}
}
}
if ($DisableProtocol) {
if ($netConf."${DisableProtocol}Enabled") {
Write-Message -Level Verbose -Message "Will disable protocol $EnableProtocol on $instance."
$netConf."${DisableProtocol}Enabled" = $false
if ($DisableProtocol -eq 'TcpIp') {
$netConf.TcpIpProperties.Enabled = $false
}
} else {
Write-Message -Level Verbose -Message "Protocol $EnableProtocol is already disabled on $instance."
}
}
if ($DynamicPortForIPAll) {
if (-not $netConf.TcpIpEnabled) {
Write-Message -Level Verbose -Message "Will enable protocol TcpIp on $instance."
$netConf.TcpIpEnabled = $true
}
if (-not $netConf.TcpIpProperties.Enabled) {
Write-Message -Level Verbose -Message "Will set property Enabled of protocol TcpIp to True on $instance."
$netConf.TcpIpProperties.Enabled = $true
}
if (-not $netConf.TcpIpProperties.ListenAll) {
Write-Message -Level Verbose -Message "Will set property ListenAll of protocol TcpIp to True on $instance."
$netConf.TcpIpProperties.ListenAll = $true
}
$ipAll = $netConf.TcpIpAddresses | Where-Object { $_.Name -eq 'IPAll' }
Write-Message -Level Verbose -Message "Will set property TcpDynamicPorts of IPAll to '0' on $instance."
$ipAll.TcpDynamicPorts = '0'
Write-Message -Level Verbose -Message "Will set property TcpPort of IPAll to '' on $instance."
$ipAll.TcpPort = ''
}
if ($StaticPortForIPAll) {
if (-not $netConf.TcpIpEnabled) {
Write-Message -Level Verbose -Message "Will enable protocol TcpIp on $instance."
$netConf.TcpIpEnabled = $true
}
if (-not $netConf.TcpIpProperties.Enabled) {
Write-Message -Level Verbose -Message "Will set property Enabled of protocol TcpIp to True on $instance."
$netConf.TcpIpProperties.Enabled = $true
}
if (-not $netConf.TcpIpProperties.ListenAll) {
Write-Message -Level Verbose -Message "Will set property ListenAll of protocol TcpIp to True on $instance."
$netConf.TcpIpProperties.ListenAll = $true
}
$ipAll = $netConf.TcpIpAddresses | Where-Object { $_.Name -eq 'IPAll' }
Write-Message -Level Verbose -Message "Will set property TcpDynamicPorts of IPAll to '' on $instance."
$ipAll.TcpDynamicPorts = ''
$port = $StaticPortForIPAll -join ','
Write-Message -Level Verbose -Message "Will set property TcpPort of IPAll to '$port' on $instance."
$ipAll.TcpPort = $port
}
if ($IpAddress) {
if (-not $netConf.TcpIpEnabled) {
Write-Message -Level Verbose -Message "Will enable protocol TcpIp on $instance."
$netConf.TcpIpEnabled = $true
}
if (-not $netConf.TcpIpProperties.Enabled) {
Write-Message -Level Verbose -Message "Will set property Enabled of protocol TcpIp to True on $instance."
$netConf.TcpIpProperties.Enabled = $true
}
if ($netConf.TcpIpProperties.ListenAll) {
Write-Message -Level Verbose -Message "Will set property ListenAll of protocol TcpIp to False on $instance."
$netConf.TcpIpProperties.ListenAll = $false
}
foreach ($ip in ($netConf.TcpIpAddresses | Where-Object { $_.Name -ne 'IPAll' })) {
if ($ip.IpAddress -match ':') {
# IPv6: Remove interface id
$address = $ip.IpAddress -replace '^(.*)%.*$', '$1'
} else {
# IPv4: Do nothing special
$address = $ip.IpAddress
}
# Is the current IP one of those to be configured?
$isTarget = $false
foreach ($listenIP in $IpAddress) {
if ($listenIP -match '^\[(.+)\]:?(\d*)$') {
# IPv6
$listenAddress = $Matches.1
$listenPort = $Matches.2
} elseif ($listenIP -match '^([^:]+):?(\d*)$') {
# IPv4
$listenAddress = $Matches.1
$listenPort = $Matches.2
} else {
Write-Message -Level Verbose -Message "$listenIP is not a valid IP address. Skipping."
continue
}
if ($listenAddress -eq $address) {
$isTarget = $true
break
}
}
if ($isTarget) {
if (-not $ip.Enabled) {
Write-Message -Level Verbose -Message "Will set property Enabled of IP address $($ip.IpAddress) to True on $instance."
$ip.Enabled = $true
}
if ($listenPort) {
# configure for static port
if ($ip.TcpDynamicPorts -ne '') {
Write-Message -Level Verbose -Message "Will set property TcpDynamicPorts of IP address $($ip.IpAddress) to '' on $instance."
$ip.TcpDynamicPorts = ''
}
if ($ip.TcpPort -ne $listenPort) {
Write-Message -Level Verbose -Message "Will set property TcpPort of IP address $($ip.IpAddress) to '$listenPort' on $instance."
$ip.TcpPort = $listenPort
}
} else {
# configure for dynamic port
if ($ip.TcpDynamicPorts -ne '0') {
Write-Message -Level Verbose -Message "Will set property TcpDynamicPorts of IP address $($ip.IpAddress) to '0' on $instance."
$ip.TcpDynamicPorts = '0'
}
if ($ip.TcpPort -ne '') {
Write-Message -Level Verbose -Message "Will set property TcpPort of IP address $($ip.IpAddress) to '' on $instance."
$ip.TcpPort = ''
}
}
} else {
if ($ip.Enabled) {
Write-Message -Level Verbose -Message "Will set property Enabled of IP address $($ip.IpAddress) to False on $instance."
$ip.Enabled = $false
}
}
}
}
$InputObject += $netConf
}
foreach ($netConf in $InputObject) {
try {
$output = [PSCustomObject]@{
ComputerName = $netConf.ComputerName
InstanceName = $netConf.InstanceName
SqlInstance = $netConf.SqlInstance
Changes = @()
RestartNeeded = $false
Restarted = $false
}
if ($Pscmdlet.ShouldProcess("Setting network configuration for instance $($netConf.InstanceName) on $($netConf.ComputerName)")) {
$computerName = Resolve-DbaComputerName -ComputerName $netConf.ComputerName -Credential $Credential
$null = Test-ElevationRequirement -ComputerName $computerName -EnableException $true
$result = Invoke-Command2 -ScriptBlock $wmiScriptBlock -ArgumentList $netConf -ComputerName $computerName -Credential $Credential -ErrorAction Stop
foreach ($verbose in $result.Verbose) {
Write-Message -Level Verbose -Message $verbose
}
$output.Changes = $result.Changes
if ($result.Exception) {
# The new code pattern for WMI calls is used where all exceptions are catched and return as part of an object.
$output.Exception = $result.Exception
Write-Message -Level Verbose -Message "Execution against $computerName failed with: $($result.Exception)"
Stop-Function -Message "Setting network configuration for instance $($netConf.InstanceName) on $($netConf.ComputerName) failed with: $($result.Exception)" -Target $netConf.ComputerName -ErrorRecord $result.Exception -Continue
}
}
if ($result.Changes.Count -gt 0) {
$output.RestartNeeded = $true
if ($RestartService) {
if ($Pscmdlet.ShouldProcess("Restarting service for instance $($netConf.InstanceName) on $($netConf.ComputerName)")) {
try {
$null = Restart-DbaService -ComputerName $netConf.ComputerName -InstanceName $netConf.InstanceName -Credential $Credential -Type Engine -Force -EnableException -Confirm:$false
$output.Restarted = $true
} catch {
Write-Message -Level Warning -Message "A restart of the service for instance $($netConf.InstanceName) on $($netConf.ComputerName) failed ($_). Restart of instance is necessary for the new settings to take effect."
}
}
} else {
Write-Message -Level Warning -Message "A restart of the service for instance $($netConf.InstanceName) on $($netConf.ComputerName) is needed for the changes to take effect."
}
}
$output
} catch {
Stop-Function -Message "Setting network configuration for instance $($netConf.InstanceName) on $($netConf.ComputerName) not possible." -Target $netConf.ComputerName -ErrorRecord $_ -Continue
}
}
}
}