|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Returns server protocol information for a SQL Server instance. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Returns server protocol information for a SQL Server instance using |
| 7 | + SMO (SQL Server Management Objects). The command supports getting |
| 8 | + information for TcpIp, NamedPipes, and SharedMemory protocols. |
| 9 | + If no protocol is specified, all protocols are returned. |
| 10 | +
|
| 11 | + .PARAMETER ServerName |
| 12 | + Specifies the name of the server where the SQL Server instance is running. |
| 13 | + Defaults to the local computer name. |
| 14 | +
|
| 15 | + .PARAMETER InstanceName |
| 16 | + Specifies the name of the SQL Server instance for which to return protocol |
| 17 | + information. |
| 18 | +
|
| 19 | + .PARAMETER ProtocolName |
| 20 | + Specifies the name of the network protocol to return information for. |
| 21 | + Valid values are 'TcpIp', 'NamedPipes', and 'SharedMemory'. |
| 22 | + If not specified, all protocols are returned. |
| 23 | +
|
| 24 | + .PARAMETER ManagedComputerObject |
| 25 | + Specifies a managed computer object from which to retrieve server protocol |
| 26 | + information. This parameter accepts pipeline input from Get-SqlDscManagedComputer. |
| 27 | +
|
| 28 | + .PARAMETER ManagedComputerInstanceObject |
| 29 | + Specifies a managed computer instance object from which to retrieve server |
| 30 | + protocol information. This parameter accepts pipeline input from |
| 31 | + Get-SqlDscManagedComputerInstance. |
| 32 | +
|
| 33 | + .EXAMPLE |
| 34 | + Get-SqlDscServerProtocol -InstanceName 'MSSQLSERVER' -ProtocolName 'TcpIp' |
| 35 | +
|
| 36 | + Returns TcpIp protocol information for the default SQL Server instance |
| 37 | + on the local computer. |
| 38 | +
|
| 39 | + .EXAMPLE |
| 40 | + Get-SqlDscServerProtocol -ServerName 'MyServer' -InstanceName 'MyInstance' -ProtocolName 'NamedPipes' |
| 41 | +
|
| 42 | + Returns NamedPipes protocol information for the MyInstance SQL Server |
| 43 | + instance on the MyServer computer. |
| 44 | +
|
| 45 | + .EXAMPLE |
| 46 | + Get-SqlDscServerProtocol -InstanceName 'MSSQLSERVER' |
| 47 | +
|
| 48 | + Returns all protocol information for the default SQL Server instance |
| 49 | + on the local computer. |
| 50 | +
|
| 51 | + .EXAMPLE |
| 52 | + Get-SqlDscManagedComputer -ServerName 'MyServer' | Get-SqlDscServerProtocol -InstanceName 'MyInstance' |
| 53 | +
|
| 54 | + Uses pipeline input from Get-SqlDscManagedComputer to retrieve all protocols |
| 55 | + for the specified instance. |
| 56 | +
|
| 57 | + .EXAMPLE |
| 58 | + Get-SqlDscManagedComputerInstance -InstanceName 'MyInstance' | Get-SqlDscServerProtocol -ProtocolName 'TcpIp' |
| 59 | +
|
| 60 | + Uses pipeline input from Get-SqlDscManagedComputerInstance to retrieve TcpIp |
| 61 | + protocol information. |
| 62 | +
|
| 63 | + .INPUTS |
| 64 | + Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer |
| 65 | +
|
| 66 | + A managed computer object can be piped to this command. |
| 67 | +
|
| 68 | + Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance |
| 69 | +
|
| 70 | + A server instance object can be piped to this command. |
| 71 | +
|
| 72 | + .OUTPUTS |
| 73 | + Microsoft.SqlServer.Management.Smo.Wmi.ServerProtocol |
| 74 | +
|
| 75 | + Returns server protocol objects from SMO (SQL Server Management Objects). |
| 76 | +#> |
| 77 | +function Get-SqlDscServerProtocol |
| 78 | +{ |
| 79 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] |
| 80 | + [CmdletBinding(DefaultParameterSetName = 'ByServerName')] |
| 81 | + [OutputType([Microsoft.SqlServer.Management.Smo.Wmi.ServerProtocol])] |
| 82 | + param |
| 83 | + ( |
| 84 | + [Parameter(ParameterSetName = 'ByServerName')] |
| 85 | + [ValidateNotNullOrEmpty()] |
| 86 | + [System.String] |
| 87 | + $ServerName, |
| 88 | + |
| 89 | + [Parameter(Mandatory = $true, ParameterSetName = 'ByServerName')] |
| 90 | + [Parameter(Mandatory = $true, ParameterSetName = 'ByManagedComputerObject')] |
| 91 | + [ValidateNotNullOrEmpty()] |
| 92 | + [System.String] |
| 93 | + $InstanceName, |
| 94 | + |
| 95 | + [Parameter(ParameterSetName = 'ByServerName')] |
| 96 | + [Parameter(ParameterSetName = 'ByManagedComputerObject')] |
| 97 | + [Parameter(ParameterSetName = 'ByManagedComputerInstanceObject')] |
| 98 | + [ValidateSet('TcpIp', 'NamedPipes', 'SharedMemory')] |
| 99 | + [System.String] |
| 100 | + $ProtocolName, |
| 101 | + |
| 102 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByManagedComputerObject')] |
| 103 | + [Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer] |
| 104 | + $ManagedComputerObject, |
| 105 | + |
| 106 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByManagedComputerInstanceObject')] |
| 107 | + [Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance] |
| 108 | + $ManagedComputerInstanceObject |
| 109 | + ) |
| 110 | + |
| 111 | + process |
| 112 | + { |
| 113 | + $previousErrorActionPreference = $ErrorActionPreference |
| 114 | + $ErrorActionPreference = 'Stop' |
| 115 | + |
| 116 | + # Set default value for ServerName if not provided |
| 117 | + if (-not $PSBoundParameters.ContainsKey('ServerName')) |
| 118 | + { |
| 119 | + $ServerName = Get-ComputerName -ErrorAction 'Stop' |
| 120 | + } |
| 121 | + |
| 122 | + if ($PSBoundParameters.ContainsKey('ProtocolName')) |
| 123 | + { |
| 124 | + Write-Verbose -Message ( |
| 125 | + $script:localizedData.ServerProtocol_GetState -f $ProtocolName, $InstanceName, $ServerName |
| 126 | + ) |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + Write-Verbose -Message ( |
| 131 | + $script:localizedData.ServerProtocol_GetAllProtocols -f $InstanceName, $ServerName |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + switch ($PSCmdlet.ParameterSetName) |
| 136 | + { |
| 137 | + 'ByServerName' |
| 138 | + { |
| 139 | + $serverInstance = Get-SqlDscManagedComputerInstance -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' |
| 140 | + } |
| 141 | + |
| 142 | + 'ByManagedComputerObject' |
| 143 | + { |
| 144 | + $serverInstance = $ManagedComputerObject | Get-SqlDscManagedComputerInstance -InstanceName $InstanceName -ErrorAction 'Stop' |
| 145 | + } |
| 146 | + |
| 147 | + 'ByManagedComputerInstanceObject' |
| 148 | + { |
| 149 | + $serverInstance = $ManagedComputerInstanceObject |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if ($PSBoundParameters.ContainsKey('ProtocolName')) |
| 154 | + { |
| 155 | + # Get specific protocol |
| 156 | + $protocolMapping = Get-SqlDscServerProtocolName -ProtocolName $ProtocolName -ErrorAction 'Stop' |
| 157 | + |
| 158 | + $serverProtocolObject = $serverInstance.ServerProtocols[$protocolMapping.ShortName] |
| 159 | + |
| 160 | + $ErrorActionPreference = $previousErrorActionPreference |
| 161 | + |
| 162 | + if (-not $serverProtocolObject) |
| 163 | + { |
| 164 | + $errorMessage = $script:localizedData.ServerProtocol_ProtocolNotFound -f $protocolMapping.DisplayName, $InstanceName, $serverInstance.Parent.Name |
| 165 | + $errorRecord = [System.Management.Automation.ErrorRecord]::new( |
| 166 | + [System.InvalidOperationException]::new($errorMessage), |
| 167 | + 'SqlServerProtocolNotFound', |
| 168 | + [System.Management.Automation.ErrorCategory]::ObjectNotFound, |
| 169 | + $ProtocolName |
| 170 | + ) |
| 171 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 172 | + } |
| 173 | + |
| 174 | + return $serverProtocolObject |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + # Get all protocols |
| 179 | + $allProtocolMappings = Get-SqlDscServerProtocolName -All -ErrorAction 'Stop' |
| 180 | + $allServerProtocols = @() |
| 181 | + |
| 182 | + foreach ($protocolMapping in $allProtocolMappings) |
| 183 | + { |
| 184 | + $serverProtocolObject = $serverInstance.ServerProtocols[$protocolMapping.ShortName] |
| 185 | + |
| 186 | + if ($serverProtocolObject) |
| 187 | + { |
| 188 | + $allServerProtocols += $serverProtocolObject |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + $ErrorActionPreference = $previousErrorActionPreference |
| 193 | + |
| 194 | + if ($allServerProtocols.Count -eq 0) |
| 195 | + { |
| 196 | + return $null |
| 197 | + } |
| 198 | + |
| 199 | + return $allServerProtocols |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments