Skip to content

Instantly share code, notes, and snippets.

@albarralnunez
Created February 16, 2016 11:41
Show Gist options
  • Save albarralnunez/7a2726ffca342032e45a to your computer and use it in GitHub Desktop.
Save albarralnunez/7a2726ffca342032e45a to your computer and use it in GitHub Desktop.
function TestPort
{
Param(
[parameter(ParameterSetName='ComputerName', Position=0)]
[string]
$ComputerName,
[parameter(ParameterSetName='IP', Position=0)]
[System.Net.IPAddress]
$IPAddress,
[parameter(Mandatory=$true , Position=1)]
[int]
$Port,
[parameter(Mandatory=$true, Position=2)]
[ValidateSet("TCP", "UDP")]
[string]
$Protocol
)
$RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};
If ($Protocol -eq 'TCP')
{
$test = New-Object System.Net.Sockets.TcpClient;
Try
{
Write-Host "Connecting to "$RemoteServer":"$Port" (TCP)..";
$test.Connect($RemoteServer, $Port);
Write-Host "Connection successful";
}
Catch
{
Write-Host "Connection failed";
}
Finally
{
$test.Dispose();
}
}
If ($Protocol -eq 'UDP')
{
Write-Host "UDP port test functionality currently not available."
<#
$test = New-Object System.Net.Sockets.UdpClient;
Try
{
Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
$test.Connect($RemoteServer, $Port);
Write-Host "Connection successful";
}
Catch
{
Write-Host "Connection failed";
}
Finally
{
$test.Dispose();
}
#>
}
}
TestPort -IPAddress 10.51.1.218 -Port 5044 -Protocol TCP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment