Holen Sie sich den Hostnamen für jede RemoteAddress-IP im neuen Mitglied

Holen Sie sich den Hostnamen für jede RemoteAddress-IP im neuen Mitglied

Ich habe ein PowerShell-Skript, das einige Variablen hat

  1. $GetCon: TCP-Verbindung in Powershell abrufen
  2. $hn: Erweitern Sie die Remote-Adresse in $GetCon.
  3. $rrt: ist eine Anzahl aller Ergebnisse, es sind alle Verbindungs-IPs.
  4. $GNamess: ist eine Variable zum Erstellen eines neuen Mitglieds anhand des Namens (URLs), $GetConwofür es sich um ein Get-NetTCPConnection handelt.

EndlichIch habe ein neues Mitglied, es wird eine Liste der Hostnamen aller Verbindungen für jede IP-Adresse in Get-TCPConnection RemoteAddress enthalten.

Aberwir beleben das Ergebnis der Hosts im Ergebnis nicht wieder, im Ergebnis habe ich einen Host für jeden Host.

Bitte besorgen Sie mir eine Methode, um alle Hosts im Ergebnis zu erhalten.

Falsche 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

Bildergebnis: Bildergebnis

Antwort1

Zu lang für einen Kommentar und Ihr Ziel lässt sich aus Ihrem Code nur schwer erraten. Könnte es also hilfreich sein, ein Füllwörterbuch zu erstellen $IPsNames?

$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

Antwort2

<# Report = LocalAddress,LocalPort,RemoteAddress,FQDN,RemotePort,PID,ProcessName,UserName,State,CreationTime #>

$obj=@()

Foreach($p In (Get-Process -IncludeUserName | where {$_.UserName} | `
 select Id, ProcessName, UserName)) {
 $properties = @{ 'PID'=$p.Id;
 'ProcessName'=$p.ProcessName;
 'UserName'=$p.UserName;
 }
 $psobj = New-Object -TypeName psobject -Property $properties
 $obj+=$psobj
 }

$Connections = Get-NetTCPConnection | where {$_.State -ne "Listen" -and $_.State -ne "Bound"} | select `
 LocalAddress, `
 LocalPort, `
 RemoteAddress, `
 @{name='FQDN';expression={ (Resolve-DnsName -name $_.Remoteaddress).NameHost}},`
 RemotePort, `
 @{n="PID";e={$_.OwningProcess}}, @{n="ProcessName";e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty ProcessName)}}, `
 @{n="UserName";e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty UserName)}}, `
 "State","CreationTime"|
 sort -Property ProcessName, UserName

 $Report += $Connections
$Connections=@()
 $Reported = $Report | Sort-Object -Property * -Unique
 $Report = $Reported




$Report |Export-Csv -Path C:\PATH\Connections.csv 

verwandte Informationen