.PARAMETER Detailed
If this switch is enabled, an object with server name, IPAddress (ipv4 and ipv6), port and static ($true/$false) for one or more SQL Servers is returned.

.PARAMETER ExcludeIpv6
If this switch is enabled, IPv6 information is excluded from detailed output.

.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.

Returns an object with server name, IPAddress (just ipv4), port and static ($true/$false) for every server listed in the Central Management Server on sql2014.
#>[CmdletBinding()]param([parameter(Mandatory=$true,ValueFromPipeline=$true)][Alias("ServerInstance","SqlServer")][DbaInstanceParameter[]]$SqlInstance,[Alias("SqlCredential")][PSCredential]$Credential,[switch]$Detailed,[Alias("Ipv4")][switch]$ExcludeIpv6,[switch][Alias('Silent')]$EnableException)process{foreach($instancein$SqlInstance){if($detailed-eq$true){try{$scriptblock={$instance=$args[0]Add-Type-AssemblyNameMicrosoft.VisualBasicforeach($servernamein$wmi.ServerInstances){$instanceName=$servername.Name$wmiinstance=$wmi.Services|Where-Object{$_.DisplayName-eq"SQL Server ($instanceName)"}$vsname=($wmiinstance.AdvancedProperties|Where-Object{$_-match'VSNAME'}).Valueif($vsname.length-eq0){$vsname="$instance\$instanceName"}$vsname=$vsname.Replace("\MSSQLSERVER","")try{$regroot=($wmiinstance.AdvancedProperties|Where-Object{$_-match'REGROOT'}).Value$dacport=(Get-ItemProperty"HKLM:\$regroot\MSSQLServer\SuperSocketNetLib\AdminConnection\Tcp").TcpDynamicPorts[PsCustomObject]@{ComputerName=$instanceInstanceName=$instanceNameSqlInstance=$vsnameIPAddress="0.0.0.0"Port=$dacportStatic=$falseType="DAC"}}catch{# it's just not our day}$tcp=$servername.ServerProtocols|Where-ObjectName-eqTcp$ips=$tcp.IPAddresses# This is a remote command so do not use Write-messageWrite-Verbose"Parsing information for $($ips.count) IP addresses."foreach($ipin$ips){$props=$ip.IPAddressProperties|Where-Object{$_.Name-eq"TcpPort"-or$_.Name-eq"TcpDynamicPorts"}foreach($propin$props){if([Microsoft.VisualBasic.Information]::IsNumeric($prop.value)){$port=$prop.valueif($prop.name-eq'TcpPort'){$static=$true}else{$static=$false}break}}[PsCustomObject]@{ComputerName=$instanceInstanceName=$instanceNameSqlInstance=$vsnameIPAddress=$ip.IPAddress.IPAddressToStringPort=$portStatic=$staticType="Normal"}}}}$computer=$instance.ComputerName$resolved=Resolve-DbaNetworkName-ComputerName$instance-Verbose:$false$computername=$resolved.FullComputerNametry{Write-Message-LevelVerbose-Message"Trying with ComputerName ($computer)."$someIps=Invoke-ManagedComputerCommand-ComputerName$computer-ArgumentList$computer-ScriptBlock$scriptblock}catch{Write-Message-LevelVerbose-Message"Trying with FullComputerName because ComputerName failed."$someIps=Invoke-ManagedComputerCommand-ComputerName$computername-ArgumentList$fqdn-ScriptBlock$scriptblock}}catch{Stop-Function-Message"Could not get detailed information."-Target$instance-ErrorRecord$_}$cleanedUp=$someIps|Sort-ObjectIPAddressif($ExcludeIpv6){$octet='(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9]{2}|2[0-5][0-5]|2[0-4][0-9])'[regex]$ipv4="^(?:$octet\.){3}$octet$"$cleanedUp=$cleanedUp|Where-Object{$_.IPAddress-match$ipv4}}$cleanedUp}if($Detailed-eq$false-or($Detailed-eq$true-and$someIps-eq$null)){try{$server=Connect-SqlInstance-SqlInstance"TCP:$instance"-SqlCredential$Credential-MinimumVersion9}catch{Stop-Function-Message"Failure"-CategoryConnectionError-ErrorRecord$_-Target$servername-Continue}# WmiComputer can be unreliable :( Use T-SQL$sql="SELECT local_tcp_port FROM sys.dm_exec_connections WHERE session_id = @@SPID"$port=$server.Query($sql)[PSCustomObject]@{ComputerName=$server.NetNameInstanceName=$server.ServiceNameSqlInstance=$server.DomainInstanceNamePort=$port.local_tcp_port}}}}}