我创建了一个脚本来输出Get-NetTCPConnection数据,但另外显示Process Name和Username。该脚本确实有效,但我喜欢任何简化或使其更规范的提示。
我想知道是否有更有效的方法将ProcessName和添加Username到输出中,而无需将值预加载到自定义 PSObject ($obj数组)中。我担心自定义e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty UserName)}}表达式过于复杂。
$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
}
Get-NetTCPConnection | where {$_.State -eq "Established"} | select `
RemoteAddress, `
RemotePort, `
@{n="PID";e={$_.OwningProcess}}, @{n="ProcessName";e={($obj |? PID -eq $_.OwningProcess | …Run Code Online (Sandbox Code Playgroud)