functions/New-DbaEndpoint.ps1
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle# function New-DbaEndpoint { <# .SYNOPSIS Creates SQL Server endpoints. .DESCRIPTION Creates SQL Server endpoints. .PARAMETER SqlInstance The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances. .PARAMETER SqlCredential Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential) .PARAMETER Name The name of the endpoint. If a name is not specified, one will be auto-generated. .PARAMETER Type The type of endpoint. Defaults to DatabaseMirroring. Options: DatabaseMirroring, ServiceBroker, Soap, TSql .PARAMETER Protocol The type of protocol. Defaults to tcp. Options: Tcp, NamedPipes, Http, Via, SharedMemory .PARAMETER Role The type of role. Defaults to All. Options: All, None, Partner, Witness .PARAMETER Port Port for TCP. If one is not provided, it will be autogenerated. .PARAMETER SslPort Port for SSL .PARAMETER EndpointEncryption Used to specify the state of encryption on the endpoint. Defaults to required. Disabled Required Supported Required by default. .PARAMETER EncryptionAlgorithm Specifies an encryption algorithm used on an endpoint. Options are: AesRC4 Aes None RC4 RC4Aes RC4 by default. .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: Endpoint 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/New-DbaEndpoint .EXAMPLE PS C:\> New-DbaEndpoint -SqlInstance localhost Creates all Endpoint(s) on the local default SQL Server instance .EXAMPLE PS C:\> New-DbaEndpoint -SqlInstance localhost, sql2016 Returns all Endpoint(s) for the local and sql2016 SQL Server instances #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] param ( [parameter(Position = 0, Mandatory, ValueFromPipeline)] [DbaInstanceParameter]$SqlInstance, [PSCredential]$SqlCredential, [string]$Name, [ValidateSet('DatabaseMirroring', 'ServiceBroker', 'Soap', 'TSql')] [string]$Type = 'DatabaseMirroring', [ValidateSet('Tcp', 'NamedPipes', 'Http', 'Via', 'SharedMemory')] [string]$Protocol = 'Tcp', [ValidateSet('All', 'None', 'Partner', 'Witness')] [string]$Role = 'All', [ValidateSet('Disabled', 'Required', 'Supported')] [string]$EndpointEncryption = 'Required', [ValidateSet('Aes', 'AesRC4', 'None', 'RC4', 'RC4Aes')] [string]$EncryptionAlgorithm = 'RC4', [int]$Port, [int]$SslPort, [switch]$EnableException ) process { if ((Test-Bound -ParameterName Name -Not)) { $name = "endpoint-" + [DateTime]::Now.ToString('s').Replace(":", "-") } foreach ($instance in $SqlInstance) { try { $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential } catch { Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue } # Thanks to https://github.com/mmessano/PowerShell/blob/master/SQL-ConfigureDatabaseMirroring.ps1 if (Test-Bound -ParameterName Port) { $tcpPort = $port } else { $thisport = (Get-DbaEndPoint -SqlInstance $server).Protocol.Tcp $measure = $thisport | Measure-Object ListenerPort -Maximum if ($null -eq $thisport) { $tcpPort = 5022 } elseif ($measure.Maximum) { $maxPort = $measure.Maximum #choose a random port that is greater than the current max port $tcpPort = $maxPort + (New-Object Random).Next(1, 500) } else { $maxPort = 5000 #choose a random port that is greater than the current max port $tcpPort = $maxPort + (New-Object Random).Next(1, 500) } } if ($Pscmdlet.ShouldProcess($server.Name, "Creating endpoint $Name of type $Type using protocol $Protocol and if TCP then using Port $tcpPort")) { try { $endpoint = New-Object Microsoft.SqlServer.Management.Smo.EndPoint $server, $Name $endpoint.ProtocolType = [Microsoft.SqlServer.Management.Smo.ProtocolType]::$Protocol $endpoint.EndpointType = [Microsoft.SqlServer.Management.Smo.EndpointType]::$Type if ($Protocol -eq "TCP") { $endpoint.Protocol.Tcp.ListenerPort = $tcpPort $endpoint.Payload.DatabaseMirroring.ServerMirroringRole = [Microsoft.SqlServer.Management.Smo.ServerMirroringRole]::$Role if (Test-Bound -ParameterName SslPort) { $endpoint.Protocol.Tcp.SslPort = $SslPort } $endpoint.Payload.DatabaseMirroring.EndpointEncryption = [Microsoft.SqlServer.Management.Smo.EndpointEncryption]::$EndpointEncryption $endpoint.Payload.DatabaseMirroring.EndpointEncryptionAlgorithm = [Microsoft.SqlServer.Management.Smo.EndpointEncryptionAlgorithm]::$EncryptionAlgorithm } $null = $endpoint.Create() $server.Endpoints.Refresh() Get-DbaEndpoint -SqlInstance $server -Endpoint $name } catch { Stop-Function -Message "Failure" -ErrorRecord $_ -Continue } } } } } |