powershell - 将 Get-NetTCPConnection 的输出写入数组 - 无输出
问题描述
我正在尝试将 PowerShell cmdlet 的值写入Get-NetTCPConnection
数组,但没有将任何内容写入列表。
$list= @()
$outputs = Get-NetTCPConnection
foreach ($output in $outputs) {
$obj = New-Object PSObject -Property @{
TheLocalAddress = "EMPTY"
TheLocalPort = "EMPTY"
TheRemoteAddress = "EMPTY"
TheRemotePort = "EMPTY"
}
$obj.TheLocalAddress = $output.LocalAddress
$obj.TheLocalPort = $output.LocalPort
$obj.TheRemoteAddress = $output.RemoteAddress
$obj.TheRemotePort = $output.RemotePort
$list += $obj
}
$list
解决方案
如果属性不需要前缀The
,为什么不使用
$list = Get-NetTCPConnection | Select-Object LocalAddress,LocalPort,RemoteAddress,RemotePort
还是更高效[PSCustomObject]
?
$list = foreach ($Conn in Get-NetTCPConnection) {
[PSCustomObject]@{
TheLocalAddress = $Conn.LocalAddress
TheLocalPort = $Conn.LocalPort
TheRemoteAddress = $Conn.RemoteAddress
TheRemotePort = $Conn.RemotePort
}
}
$list
推荐阅读
- jenkins - jenkins gradle 集成需要一些帮助
- mysql - 我的服务器不支持 mysql 中的 JSON 字段
- angular - 从 Angular 2.2 到 Angular 6.0 的角度更新
- python - 我正在使用 python3,我有字典列表
- javascript - 通过动态枚举其他对象的键来创建新对象
- c - 如何将字符串从函数返回到 main()?
- java - 在 Json 反序列化期间维护类字段的顺序
- spring-boot - 在类型 [java.util.Optional] 上找不到属性 [id]
- php - 比较2个对象数组php并返回匹配
- android - 无法在 android studio 中加载 .so