Can't get a count of 1` from get-nettcpconnection for remote computer in powershell

Jacque Mergens 1 Reputation point
2022-11-17T16:53:26.38+00:00

I am trying to get a count of connections from a remote server.

For some reason I don't get a return value at all when I have a single connection.
0 connections works fine as well as anything more that 1 but a single connection will not return a count.

When I try to get a count of connections to my remote computer here is what happens:

Without Count and a single connection:
PS C:\Users\jmergens\Desktop\Auto-Scale> Invoke-Command -ComputerName CPN1 -ScriptBlock {(get-nettcpconnection -state established -localport 443 -ErrorAction silentlycontinue)}

LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProcess PSComputerName

------------
--------- ------------- ---------- ----- -------------- ------------- --------------
172. 18.21.134 443 172.18.12.141 51082 5 1 4 CPN1

With Count and a single connection:
PS C:\Users\jmergens\Desktop\Auto-Scale> Invoke-Command -ComputerName CPN1 -ScriptBlock {(get-nettcpconnection -state established -localport 443 -ErrorAction silentlycontinue).count}

With Count and 4 connections:
PS C:\Users\jmergens\Desktop\Auto-Scale> Invoke-Command -ComputerName CPN1 -ScriptBlock {(get-nettcpconnection -state established -localport 443 -ErrorAction silentlycontinue).count}
4

Without Count and 4 connections:
PS C:\Users\jmergens\Desktop\Auto-Scale> Invoke-Command -ComputerName CPN1 -ScriptBlock {(get-nettcpconnection -state established -localport 443 -ErrorAction silentlycontinue)}

LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProcess PSComputerName

------------
--------- ------------- ---------- ----- -------------- ------------- --------------
172. 18.21.134 443 172.18.12.141 51082 5 1 4 CPN1
172. 18.21.134 443 172.18.21.134 50308 5 1 4 CPN1
172. 18.21.134 443 172.18.12.141 51181 5 1 4 CPN1
172. 18.21.134 443 172.18.21.134 50313 5 1 4 CPN1

Can anyone help me with this?
I tried this on 5.1 and 7.3

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2022-11-17T19:09:46.817+00:00

    The Get-NetTCPConnection returns one, or more, CimInstance objects. If there are more than one returned the result is an array. If only one is returned the result is a single object. An array has a "count" method. The CimInstance object does not (nor does it inherit one).

    Change the contents of the script block to:

    [array](get-nettcpconnection -state established -localport 443 -ErrorAction silentlycontinue).count  
    

    Coercing the output to be an array, even if it's empty or contains a single element is the way to handle this.

    0 comments No comments