Get HostName for Each RemoteAddress IP in New Member

1

I have a PowerShell script, which has some variables

  1. $GetCon : Get Tcp Connection in Powershell
  2. $hn : Get expand Remote-Address in $GetCon.
  3. $rrt : is a Number of all results, it's all connection IP's.
  4. $GNamess : is a variable to Create a new Member by name (urls) for $GetCon which is a Get-NetTCPConnection.

Finally I have a new member, it will be contain list of each Connections Host Names for Each IP Address in Get-TCPConnection RemoteAddress.

But we don't revive result of host's in the result, in the result I've one Host for each host's.

please get me an method to get all host's in the result.

Wrong Syntax:

$GetCon = Get-NetTCPConnection

$hn = $GetCon | select -expand RemoteAddress

$rrt = foreach ($IPs in $hn)
{

 [System.Net.Dns]::GetHostAddresses($IPs) | select-object IPAddressToString -expandproperty  IPAddressToString

}


$GNamess = foreach ($IPst in $GetCon) {

    $rrt = ([System.Net.Dns]::GetHostbyAddress($IPs) | select-object HostName -expandproperty  HostName)
    $IPst | Add-Member -NotePropertyName urls -NotePropertyValue $rrt -PassThru
}

$GetCon | select urls

Image Result: Image Result

SchoolforDesign

Posted 2017-01-13T14:09:19.287

Reputation: 145

Answers

1

Too long for comment and hard to guess your aim from your code so maybe creating an filling dictionary $IPsNames could help?

$IPsNames = @{}

$GetCon = Get-NetTCPConnection

$hn = $GetCon | select -expand RemoteAddress  | sort -Unique

foreach ( $IPs in $hn ) {

    try   { $rrtx = [System.Net.Dns]::GetHostbyAddress($IPs).HostName }
    catch { $rrtx = '???' }

    $IPsNames[ $IPS ] = $rrtx
}

### $IPsNames

for ( $i = 0; $i -lt $GetCon.Count; $i++ ) {

    $aux = $IPsNames[ $GetCon[$i].RemoteAddress ]
    $GetCon[$i] | Add-Member -NotePropertyName urls -NotePropertyValue $aux 

}

### $GetCon | Format-Table -Property RemoteAddress, urls -AutoSize

$GetCon | selelect -Property RemoteAddress, urls

JosefZ

Posted 2017-01-13T14:09:19.287

Reputation: 9 121

Thank yo @JosefZ to help our errors. this helpful for me, but I want to Add new Member to $GetCon variable. I have a Wrong syntax with your Right syntax: $Rem = $IPsNames.Values $GetCon | select urls ... Please also fix this for me. thank you – SchoolforDesign – 2017-01-13T18:04:39.640

@SchoolforDesign answer updated (possible approach). – JosefZ – 2017-01-13T19:52:18.350

Thank you my bro, it's very helpful for me... @JosefZ Can I ask something else? – SchoolforDesign – 2017-01-17T00:05:41.837

@SchoolforDesign Of course, you can ask here for a minor specification… although… this strategy could be a more effective way for you as StackExchage sites are not chat-like ones, ultimately. If my answer was helpful, please consider marking it as accepted. See this page for an explanation of why this is important.

– JosefZ – 2017-01-17T01:35:05.617

Of course @JosefZ, your answer is very helpful, sorry for forgetting an accepted you answer. (1) my other question is: i have a HTML - table on my powershell which is imported some case or some aliases such as port, ipaddress..etc. and i have an HTML <div> tag which contain nothing yet! i want to add some information embeded in $GetCon which is my Aliases. – SchoolforDesign – 2017-01-17T13:44:16.877

(2) , i want to add for example: when I click first row in table, which contain IP(127.0.0.1), the <div> containing a 'localhost'. and so on for each oter table rows. thank you again. – SchoolforDesign – 2017-01-17T13:44:26.127