functions/Set-DbaTcpPort.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
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#

Function Set-DbaTcpPort {
<#
    .SYNOPSIS
        Changes the TCP port used by the specified SQL Server.
     
    .DESCRIPTION
        This function changes the TCP port used by the specified SQL Server.
     
    .PARAMETER SqlInstance
        The SQL Server that you're connecting to.
     
  .PARAMETER SqlCredential
        Credential object used to connect to the SQL Server instance as a different user
  
    .PARAMETER Credential
        Credential object used to connect to the Windows server itself as a different user
     
    .PARAMETER Port
        TCPPort that SQLService should listen on.
     
    .PARAMETER IpAddress
        Wich IpAddress should the portchange , if omitted allip (0.0.0.0) will be changed with the new portnumber.
     
    .PARAMETER Silent
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
     
    .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.
     
    .EXAMPLE
        Set-DbaTcpPort -SqlInstance SqlInstance2014a -Port 1433
         
        Sets the port number 1433 for allips on the default instance on SqlInstance2014a
     
    .EXAMPLE
        Set-DbaTcpPort -SqlInstance winserver\sqlexpress -IpAddress 192.168.1.22 -Port 1433
         
        Sets the port number 1433 for IP 192.168.1.22 on the sqlexpress instance on winserver
     
    .EXAMPLE
        Set-DbaTcpPort -SqlInstance 'SQLDB2014A' ,'SQLDB2016B' -port 1337
         
        Sets the port number 1337 for ALLIP's on SqlInstance SQLDB2014A and SQLDB2016B
     
    .NOTES
        Author: Hansson7707@gmail.com, @H0s0n77
         
        Website: https://dbatools.io
        Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
        License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
     
    .LINK
        https://dbatools.io/Set-DbaTcpPort
#>

    [CmdletBinding(ConfirmImpact = "High")]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [PSCredential]$Credential,
        [parameter(Mandatory = $true)]
        [ValidateRange(1, 65535)]
        [int]$Port,
        [IpAddress[]]$IpAddress,
        [switch]$Silent
    )
    
    begin {
        
        if ($IpAddress.Length -eq 0) {
            $IpAddress = '0.0.0.0'
        }
        else {
            if ($SqlInstance.count -gt 1) {
                Stop-Function -Message "-IpAddress switch cannot be used with a collection of serveraddresses" -Target $SqlInstance
                return
            }
        }
    }
    
    process {
        if (Test-FunctionInterrupt) { return }
        
        foreach ($instance in $SqlInstance) {
            
            try {
                Write-Message -Level Verbose -Message "Connecting to $instance"
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }
            
            $wmiinstancename = $server.ServiceName
            
            
            if ($server.IsClustered) {
                Write-Message -Level Verbose -Message "Instance is clustered fetching nodes..."
                $clusternodes = (Get-DbaClusterActiveNode -SqlInstance $server -Detailed).NodeName -join ", "
                
                Write-Message -Level Output -Message "$instance is a clustered instance, portchanges will be reflected on all nodes ($clusternodes) after a failover"
            }
            
            $scriptblock = {
                $instance = $args[0]
                $wmiinstancename = $args[1]
                $port = $args[2]
                $IpAddress = $args[3]
                $sqlinstanceName = $args[4]
                
                $wmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $instance
                $wmiinstance = $wmi.ServerInstances | Where-Object { $_.Name -eq $wmiinstancename }
                $tcp = $wmiinstance.ServerProtocols | Where-Object { $_.DisplayName -eq 'TCP/IP' }
                $IpAddress = $tcp.IpAddresses | where-object { $_.IpAddress -eq $IpAddress }
                $tcpport = $IpAddress.IpAddressProperties | Where-Object { $_.Name -eq 'TcpPort' }
                
                $oldport = $tcpport.Value
                try {
                    $tcpport.value = $port
                    $tcp.Alter()
                    [pscustomobject]@{
                        ComputerName = $env:COMPUTERNAME
                        InstanceName = $wmiinstancename
                        SqlInstance = $sqlinstanceName
                        OldPortNumber = $oldport
                        PortNumber = $Port
                        Status = "Success"
                    }
                }
                catch {
                    [pscustomobject]@{
                        ComputerName = $env:COMPUTERNAME
                        InstanceName = $wmiinstancename
                        SqlInstance = $sqlinstanceName
                        OldPortNumber = $oldport
                        PortNumber = $Port
                        Status = "Failed: $_"
                    }
                }
            }
            
            try {
                $computerName = $instance.ComputerName
                $resolved = Resolve-DbaNetworkName -ComputerName $computerName
                
                Write-Message -Level Verbose -Message "Writing TCPPort $port for $instance to $($resolved.FQDN)..."
                Invoke-ManagedComputerCommand -ComputerName $resolved.FQDN -ScriptBlock $scriptblock -ArgumentList $Server.NetName, $wmiinstancename, $port, $IpAddress, $server.DomainInstanceName -Credential $Credential
                
            }
            catch {
                Invoke-ManagedComputerCommand -ComputerName $server.ComputerNamePhysicalNetBIOS -ScriptBlock $scriptblock -ArgumentList $Server.NetName, $wmiinstancename, $port, $IpAddress, $server.DomainInstanceName -Credential $Credential
            }
        }
    }
}