Add-Member : The input object cannot be bound to any parameters

Good day,

Noob here:

I am trying to get a list of open ports and then using the PID to lookup processname and username, all packed in to an array:

[array]$ports = get-nettcpconnection | Select-object state, creationtime, localaddress, localport, owningprocess, remoteport, remoteaddress
foreach($i in $ports.owningprocess)
{
Get-Process -Id $i -IncludeUserName | 
Add-Member -inputobject $ports -NotePropertyMembers @{
processname = $_.processname
username = $_.username}

}

but i just get the following error:

“Add-Member : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:5 char:1”

What am I doing wrong here? Or is there an entirely different and better way of doing this?

 

Thank you in advance

Regards

Hello @hanskapans,
Couple of comments:

  1. On the first line you are selecting only 3 properties ( State, CreationTime, LocalAddress) and then in the foreach loop you are using OwningProcess
  2. When you are trying to Add-Member, the Add-Member cmdlet should be the first one in the pipeline.

I would suggest the following:

#Create Collection of PSCustom Objects with only needed properties from get-nettcpconnection
#Loop through all ports and use Add-Member to add User and Process name

Quick & dirty way:

$PortsList=New-Object System.Collections.Generic.List[PSObject]

$ports = get-nettcpconnection 
foreach($i in $ports)
{


    $ProcessInfo=Get-Process -Id $i.owningprocess -IncludeUserName
    Add-Member -inputobject $ports -NotePropertyMembers @{
    processname = $_.processname
    username = $_.username}

    $prop=[ordered]@{
        state=$i.state
        creationtime=$i.creationtime
        localaddres=$i.localaddres
        processname=$ProcessInfo.processname
        username=$ProcessInfo.username
    }
    $obj=New-Object -TypeName psobject -Property $prop
    $PortsList.Add($Obj)

}

Output:

Hope that helps.

I removed the Add-Member cmdlet within the foreach loop

$ports = Get-NetTCPConnection
$PortsList = foreach($i in $ports)
{
    $ProcessInfo= Get-Process -Id $i.owningprocess -IncludeUserName

    $prop=[ordered]@{
        State           =   $i.State
        CreationTime    =   $i.CreationTime
        LocalAddress    =   $i.LocalAddress
        ProcessName     =   $ProcessInfo.ProcessName
        Username        =   $ProcessInfo.Username
    }
    New-Object -TypeName pscustomobject -Property $prop
}

$PortsList | Format-Table -AutoSize
1 Like