
몇 가지 변수가 있는 PowerShell 스크립트가 있습니다.
$GetCon
: Powershell에서 Tcp 연결 가져오기$hn
: 에서 원격 주소 확장을 가져옵니다$GetCon
.$rrt
:는 모든 결과의 수이며 모든 연결 IP입니다.$GNamess
: 이름으로 새 멤버를 생성하기 위한 변수입니다(URL)에 대한$GetCon
Get-NetTCPConnection입니다.
마지막으로새 구성원이 있는데 여기에는 Get-TCPConnection RemoteAddress의 각 IP 주소에 대한 각 연결 호스트 이름 목록이 포함됩니다.
하지만우리는 결과에서 호스트의 결과를 부활시키지 않습니다. 결과적으로 각 호스트에 대해 하나의 호스트가 있습니다.
결과에서 모든 호스트를 얻을 수 있는 방법을 알려주세요.
잘못된 구문:
$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
이미지 결과: 이미지 결과
답변1
설명이 너무 길고 코드에서 목표를 추측하기 어렵기 때문에 채우기 사전을 만드는 것이 $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
답변2
<# 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