gpt4 book ai didi

powershell - 如何选择特定列

转载 作者:行者123 更新时间:2023-12-04 00:29:17 28 4
gpt4 key购买 nike

我需要提取特定列的值。我需要解析使用 cmd 生成的输出:

netstat -an |

Select-String "TCP\s+.+\:.+\s+(.+)\:(\d+)\s+(\w+)" |

ForEach-Object {

$key = $_.matches[0].Groups[1].value

$Status = $_.matches[0].Groups[3].value.

打印时超过 2 个字符串给了我外部 IP 和连接状态。我需要一个端口号为本地 IP 的列,外部 IP 连接到该列。

最佳答案

如果您在 Windows 8 或 Windows Server 2012 上运行,您可以在 Powershell V3 中使用以下命令示例:

使用 Select-Object 进行流水线化

Get-NetTCPConnection | Select LocalAddress,LocalPort,RemoteAddress,RemotePort

单独选择每个属性

(Get-NetTCPConnection).LocalAddress
(Get-NetTCPConnection).LocalPort
(Get-NetTCPConnection).RemoteAddress
(Get-NetTCPConnection).RemoteAddress

使用每个属性创建变量

$LocalAddress = (Get-NetTCPConnection).LocalAddress
$LocalPort = (Get-NetTCPConnection).LocalPort
$Remote Address = (Get-NetTCPConnection).RemoteAddress
$RemotePort = (Get-NetTCPConnection).RemoteAddress

这些都应该以列表的形式出现。

希望这会有所帮助:)

关于powershell - 如何选择特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6395875/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com