4
3
Introduction
Aim: to check whether a port is in LISTENING state on localhost
by using a PowerShell function
The following command:
New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)
results in:
PS C:\Windows\system32> New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)
At line:1 char:33
+ New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
PS C:\Windows\system32>
Question
Which PowerShell Function could be used to check whether a certain port is in LISTENING state on localhost?
Are you trying to see if the port on the local machine is in LISTENING state, or are you trying to catch the error when the TCPClient.Connect fails to connect to a host? These are two very different things. :) PS: try adding brackets:
(New-Object Net.Sockets.TcpClient).Connect("127.0.0.1",10389)
, as this will change the order of operations and instantiate the new socket object before calling Connect on it. Or split it up into two separate commands using a variable. – Ƭᴇcʜιᴇ007 – 2014-06-17T17:17:45.253Question has been updated. – 030 – 2014-06-17T17:19:32.450