function Add-NetshExcludedPortRange { <# .SYNOPSIS This function is used to add a specific starting and number of ports to the system. This is a very thing wrapper around the netsh tool for adding IPv4 ports. .PARAMETER Start This is the starting port number. .PARAMETER NumberOfPorts This is the count of ports in the given range. .PARAMETER End This is the end port number when providing a range. .EXAMPLE Add-NetshExcludedPortRange -Start 50 -End 55 .EXAMPLE Add-NetshExcludedPortRange -Start 50 -NumberOfPorts 6 #> [CmdletBinding(DefaultParameterSetName = 'NumberOfPorts')] [OutputType([System.Boolean])] param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [Alias("StartPort")] [int]$Start, [Parameter(Mandatory = $true, ParameterSetName = 'NumberOfPorts')] [ValidateNotNullOrEmpty()] [Alias("Range")] [int]$NumberOfPorts, [Parameter(Mandatory = $true, ParameterSetName = 'EndPorts')] [ValidateNotNullOrEmpty()] [Alias("EndPort")] [int]$End ) $logLead = (Get-LogLeadName) if ($Start -le 0) { throw "$logLead : Start port value must be greater than 0" } if ($PSCmdlet.ParameterSetName -eq 'NumberOfPorts') { if ($NumberOfPorts -eq $Start) { throw "$logLead : NumberOfPorts can not equal the parameter for Start" } if ($NumberOfPorts -le 0) { throw "$logLead : NumberOfPorts value must be greater than 0" } } if ($PSCmdlet.ParameterSetName -eq 'EndPorts') { if ($End -le $Start) { throw "$logLead : End port value must be larger than Start port value" } if ($End -le 0) { throw "$logLead : End port value must be greater than 0" } $NumberOfPorts = $End - $Start + 1 } Write-Host "$logLead : Creating ipv4/tcp excludedPortRange for Start port [$Start] for [$NumberOfPorts] ports" $output = netsh int ipv4 Add excludedportrange protocol=tcp startport=$Start numberofports=$NumberOfPorts if ($output -match "error") { Write-Error "$logLead : Failed to add port range`r`n$output" return $false } return $true }