public/interface/portproxy/Get-PortProxy.ps1
function Get-PortProxy { <# .SYNOPSIS Gets v4tov4 portproxy entries using 'netsh interface portproxy' .DESCRIPTION Gets v4tov4 portproxy entries using netsh. The portproxy feature in Windows enables you to accept TCP connections on a specific interface (or all interfaces) and a specific port, then redirect that connection to another IP and optionally a different port. Effectively this turns your Windows PC into a very simple proxy server. It's important to note that there is no authentication or security associated with the portproxy feature, so all inbound TCP traffic will be sent out to the supplied ConnectAddress unless you have created firewall rules to limit the allowed sources for inbound connections. .EXAMPLE PS C:\> Get-PortProxy -ListenPort 8000 Returns the portproxy entry (or entries) with listenport=8000 .OUTPUTS [pscustomobject] with properties 'InternetProtocol', 'ListenAddress', 'ListenPort', 'ConnectAddress', 'ConnectPort'. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # Specifies the IP or hostname on which incoming connections are being accepted. Results will be filtered to match this value if this optional parameter is provided. [Parameter(ValueFromPipelineByPropertyName)] [string] $ListenAddress = "*", # Specifies the TCP port on which incoming connections are being accepted. Results will be filtered to match this value if this optional parameter is provided. [Parameter(ValueFromPipelineByPropertyName)] [ValidateRange(0,65535)] [int] $ListenPort, # Specifies the IP or hostname to which incoming connections are being proxied. Results will be filtered to match this value if this optional parameter is provided. [Parameter(ValueFromPipelineByPropertyName)] [string] $ConnectAddress = "*", # Specifies the TCP port to which incoming connections are being proxied. Results will be filtered to match this value if this optional parameter is provided. [Parameter(ValueFromPipelineByPropertyName)] [ValidateRange(0,65535)] [int] $ConnectPort ) process { try { $listenAddressPattern = [WildcardPattern]::new($ListenAddress, [System.Management.Automation.WildcardOptions]::IgnoreCase) $connectAddressPattern = [WildcardPattern]::new($ConnectAddress, [System.Management.Automation.WildcardOptions]::IgnoreCase) $pattern = '^\s*(?<ListenAddress>[^\s]+)\s+(?<ListenPort>\d+)\s+(?<ConnectAddress>[^\s]+)\s+(?<ConnectPort>\d+)\s*$' $command = "netsh.exe interface portproxy show v4tov4" Write-Verbose "Executing the command '$command'" $output = Invoke-Expression -Command $command $entriesMatchingParameters = 0 $output | Where-Object { $_ -match $pattern } | ForEach-Object { if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ListenAddress') -and -not $listenAddressPattern.IsMatch($Matches.ListenAddress)) { # no-op } elseif ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ListenPort') -and $Matches.ListenPort -ne $ListenPort.ToString()) { # no-op } elseif ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ConnectAddress') -and -not $connectAddressPattern.IsMatch($Matches.ConnectAddress)) { # no-op } elseif ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ConnectPort') -and $Matches.ConnectPort -ne $ConnectPort.ToString()) { # no-op } else { $entriesMatchingParameters += 1 [PortProxy]@{ InternetProtocol = "v4tov4" ListenAddress = $Matches.ListenAddress ListenPort = [int]::Parse($Matches.ListenPort) ConnectAddress = $Matches.ConnectAddress ConnectPort = [int]::Parse($Matches.ConnectPort) } } } if ($entriesMatchingParameters -eq 0 -and (![WildcardPattern]::ContainsWildcardCharacters($ListenAddress) -or ![WildcardPattern]::ContainsWildcardCharacters($ConnectAddress) -or $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ListenPort') -or $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('ConnectPort'))) { $message = "No portproxy entries found matching the given parameters: listenport=$ListenPort connectaddress=$ConnectAddress listenaddress=$ListenAddress connectport=$ConnectPort" Write-Error -Message $message -Category ObjectNotFound } } catch { Write-Error $_ } } } |