如何使用 PowerShell 查找活动设备 ID?

How to find active device id with PowerShell?

我正在尝试自动安装 Paketbeat,但 Windows 上的一项要求是您需要找到活动网络适配器的设备 ID。 可以用.\packetbeat devices.

查询设备列表

示例输出为:

PS C:\Program Files\packetbeat> .\packetbeat.exe devices
0: \Device\NPF_NdisWanIp (NdisWan Adapter) (Not assigned ip address)
1: \Device\NPF_NdisWanBh (NdisWan Adapter) (Not assigned ip address)
2: \Device\NPF_{DD2F4800-0DEB-4A98-A302-0777CB955DC1} (AsyncMac Adapter) (Not assigned ip address)
3: \Device\NPF_NdisWanIpv6 (NdisWan Adapter) (Not assigned ip address)
4: \Device\NPF_{5B8B7F6A-EF39-4D95-A3A5-4BF70077E936} (VMware vmxnet3 virtual network device) (12.54.26.105)
5: \Device\NPF_{B8522370-3DA7-4F29-91FC-0718181D5661} (MS LoopBack Driver) (0.0.0.0)

在上述用例中,我需要检索 4。 或者

PS C:\Program Files\packetbeat> .\packetbeat.exe devices
0: \Device\NPF_NdisWanIp (NdisWan Adapter) (Not assigned ip address)
1: \Device\NPF_NdisWanBh (NdisWan Adapter) (Not assigned ip address)
2: \Device\NPF_{DD2F4800-0DEB-4A98-A302-0777CB955DC1} (AsyncMac Adapter) (Not assigned ip address)
3: \Device\NPF_{8E8A32C0-6E4D-46ED-9723-9D656A26D1F5} (EMULEX) (12.54.18.145)
4: \Device\NPF_NdisWanIpv6 (NdisWan Adapter) (Not assigned ip address)
5: \Device\NPF_{83485D06-422D-4558-AC88-5D0EB800BB1C} (MS LoopBack Driver) (fe80::ezeb:459b:61a4:c175 0.0.0.0)
PS C:\Program Files\packetbeat> .\packetbeat.exe devices | Select-Object

在上述用例中,我需要检索 3。我很想根据设备 ID 使用 PowerShell 查找 ID,该设备 ID 配置的 IP 以 12.45.

开头

使用 -match 运算符过滤以匹配 IP 地址结尾的行的输出,然后在冒号处拆分该行并从结果数组中选择第一个元素:

((packetbeat.exe devices) -match '\(12\.54\.\d+\.\d+\)$' -split ':')[0]