Powershell 创造一场前瞻性的比赛

Powershell 创造一场前瞻性的比赛,powershell,powershell-3.0,Powershell,Powershell 3.0,玩PS游戏,我有一个简单的脚本 ipconfig /all | where-object {$_ -match "IPv4" -or $_ -match "Description"} 这是伟大的,做了我所期望的。我想做的是提前阅读,只显示IPv4行前面的描述。或者反向搜索并获取ipv4和下一个描述,然后查找下一个ipv4等 有没有一种方法可以在不旋转的情况下通过创建数组然后旋转数组来提取有意义的部分 我的笔记本电脑上的此命令会导致: Description . . . . . . . . .

玩PS游戏,我有一个简单的脚本

ipconfig /all | where-object {$_ -match "IPv4" -or  $_ -match "Description"}
这是伟大的,做了我所期望的。我想做的是提前阅读,只显示IPv4行前面的描述。或者反向搜索并获取ipv4和下一个描述,然后查找下一个ipv4等

有没有一种方法可以在不旋转的情况下通过创建数组然后旋转数组来提取有意义的部分

我的笔记本电脑上的此命令会导致:

Description . . . . . . . . . . . : Microsoft Virtual WiFi Miniport Adapter
Description . . . . . . . . . . . : Killer Wireless-N 1103 Network Adapter
IPv4 Address. . . . . . . . . . . : 192.168.1.2(Preferred) 
Description . . . . . . . . . . . : Atheros AR8151 PCI-E Gigabit Ethernet Controller (NDIS 6.20)
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
IPv4 Address. . . . . . . . . . . : 192.168.122.1(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
IPv4 Address. . . . . . . . . . . : 192.168.88.1(Preferred) 
Description . . . . . . . . . . . : Microsoft ISATAP Adapter
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #4
Description . . . . . . . . . . . : Microsoft ISATAP Adapter #5
我想要的是:

Description . . . . . . . . . . . : Killer Wireless-N 1103 Network Adapter
IPv4 Address. . . . . . . . . . . : 192.168.1.2(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
IPv4 Address. . . . . . . . . . . : 192.168.122.1(Preferred) 
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
IPv4 Address. . . . . . . . . . . : 192.168.88.1(Preferred) 

如果要提取启用IPv4的适配器的所有描述,可以尝试以下操作:

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % {
    $_.Context.Precontext -match "Description" -replace 'Description(?:[^:]+):(.*)$', '$1'
}
    Intel(R) 82579V Gigabit Network Connection
要使用您的代码获取它,请尝试以下操作:

ipconfig /all | where-object {
    $_ -match "IPv4" -or  $_ -match "Description"
} | Select-String "IPv4" -SimpleMatch -AllMatches -Context 1 | % { 
    $_.context.precontext -replace 'Description(?:[^:]+):(.*)$', '$1'
}
编辑对不起,我之前似乎误读了你的问题。我以为你只想要描述。这显示了IPv4活动适配器的说明和IP线路

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % {
    $_.Context.Precontext -match "Description"
    $_.Line
}

Description . . . . . . . . . . . : Intel(R) 82579V Gigabit Network Connection
IPv4 Address. . . . . . . . . . . : xx.xx.xx.xx(Preferred) 

如果要提取启用IPv4的适配器的所有描述,可以尝试以下操作:

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % {
    $_.Context.Precontext -match "Description" -replace 'Description(?:[^:]+):(.*)$', '$1'
}
    Intel(R) 82579V Gigabit Network Connection
要使用您的代码获取它,请尝试以下操作:

ipconfig /all | where-object {
    $_ -match "IPv4" -or  $_ -match "Description"
} | Select-String "IPv4" -SimpleMatch -AllMatches -Context 1 | % { 
    $_.context.precontext -replace 'Description(?:[^:]+):(.*)$', '$1'
}
编辑对不起,我之前似乎误读了你的问题。我以为你只想要描述。这显示了IPv4活动适配器的说明和IP线路

ipconfig /all | Select-String "IPv4" -AllMatches -SimpleMatch -Context 5 | % {
    $_.Context.Precontext -match "Description"
    $_.Line
}

Description . . . . . . . . . . . : Intel(R) 82579V Gigabit Network Connection
IPv4 Address. . . . . . . . . . . : xx.xx.xx.xx(Preferred) 
替代解决方案:

[regex]$regex = '(?ms)^\s*(Description[^\r]+\r\n\s*IPv4[^\r]+)\r'
$regex.matches(((ipconfig /all) -match '^\s*Description|IPv4') -join "`r`n") |
foreach {$_.groups[1].value -replace '\. ',''}
替代解决方案:

[regex]$regex = '(?ms)^\s*(Description[^\r]+\r\n\s*IPv4[^\r]+)\r'
$regex.matches(((ipconfig /all) -match '^\s*Description|IPv4') -join "`r`n") |
foreach {$_.groups[1].value -replace '\. ',''}

另一个选项,它只是跟踪在输出中找到的最后一个描述:

switch-regex(ipconfig/all){'IPv4'{$d+$\}'说明'{$d=@($\}}


此外,可以处理数组和单个字符串。因此,使用
(ipconfig/all)-match'IPv4 | Description'
相当于原始的
ipconfig/all{$|其中{$|-match'IPv4'-或$|-match'Description'}
分别检查每一行。

另一个选项,它只是跟踪在输出中找到的最后一个描述:

switch-regex(ipconfig/all){'IPv4'{$d+$\}'说明'{$d=@($\}}


此外,可以处理数组和单个字符串。因此,使用
(ipconfig/all)-match'IPv4 | Description'
相当于原始的
ipconfig/all{$\u-match'IPv4'-或$\u-match'Description'}
分别检查每一行。

这将显示没有IP的活动适配器,这些适配器也可以被使用+1我添加了另一个解决方案。一开始我一定误读了你的问题。不过,现在您有了一些选择+一个如何使用正则表达式提取描述的示例(IP是通过将“description”替换为“IP”并删除正则表达式中的美元符号来完成的):)这是一个很好的替代方案,使用不同的方法,我可以选择在“我的”工具箱中使用PS作为相关工具。这会显示没有IP的活动适配器,也会很有用+1我添加了另一个解决方案。一开始我一定误读了你的问题。不过,现在您有了一些选择+一个如何使用正则表达式提取描述的示例(IP是通过将“description”替换为“IP”并删除正则表达式中的美元符号来完成的):)这是一个很好的选择,使用不同的方法,我可以选择PS作为“我的”工具箱中的相关工具。+1这正好提供了我在原始帖子中要求的内容。这是另一种有助于使PS与我相关的方法。我根本没有在PS中使用开关。+1这正是我在原始帖子中要求的。这是另一种有助于使PS与我相关的方法。我根本没有在PS中使用开关。