Select-Object 选择要显示的对象。它支持“计算属性”。$Process定义了一个名为“Process”的计算属性:它采用原始的 OwningProcess 属性,并通过 Get-Process 运行其中的进程 ID,以获取应用程序的路径。
$Process = @{
Name='Process'
Expression={
# return process path
(Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).Path
}
}
$HostName = @{
Name='Host'
Expression={
$remoteHost = $_.RemoteAddress
try {
# try to resolve IP address
[Net.Dns]::GetHostEntry($remoteHost).HostName
} catch {
# if that fails, return IP anyway
$remoteHost
}
}
}
# get all connections to port 443 (HTTPS)
Get-NetTCPConnection -RemotePort 443 -State Established |
# where there is a remote address
Where-Object RemoteAddress |
# and resolve IP and process ID
Select-Object -Property $HostName, OwningProcess, $Process