This might take you < 1 minute to read.

In Windows 2012R2 and Windows 8.1 there is a command “Test-NetConnect” which tests a TCP port. That is a life saver. To be able to do that in Windows 2012 I created a small function.

What it does is to take two parameters. first is an IPaddress and the second one is the tcp-port.

Then it connects to the tcp-socket and will prompt with a result, if it was able to connect or not.

Function test-IP-Port {
	param (
		[Parameter(Mandatory=$true)]
		[ValidateScript({$_ -match [IPAddress]$_ })]  
        [String] $Ipaddress,
		[Parameter(Mandatory=$true)]
		[Int] $Port
	)
	$netSocket = New-Object Net.Sockets.TcpClient
	$netSocket.Connect($Ipaddress,$Port)
    if($netSocket.Connected)
    {
        "Port $Port on $IPAddress is listening."
    }
    else
    {
        "Port $Port is closed on $IPAddress is closed. "
    }
	$netsocket.Close()
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.