从行输出powershell中删除字
我有一个带以下输出的powershell命令,该命令输出显示每台机器上的活动NIC适配器和NIC适配器名称不同。但我在这里看到的是,在一个服务器中,活动NIC适配器是局域网连接,而在另一个服务器中,它是以太网。这在所有VM中都是不同的从行输出powershell中删除字,powershell,Powershell,我有一个带以下输出的powershell命令,该命令输出显示每台机器上的活动NIC适配器和NIC适配器名称不同。但我在这里看到的是,在一个服务器中,活动NIC适配器是局域网连接,而在另一个服务器中,它是以太网。这在所有VM中都是不同的 PS C:\> netsh interface ipv4 show interface |where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'} 12 5
PS C:\> netsh interface ipv4 show interface |where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
12 5 1500 connected **Local Area Connection**
PS C:\>
PS C:\> netsh interface ipv4 show interface |where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
12 5 1500 connected **Ethernet**
PS C:\>
我只希望捕获适配器名称(例如:局域网连接\以太网等)
有人能帮我修改命令吗?这样我就可以获得NIC适配器名称,而不需要任何空白作为输出
输出应该如下所示
Local Area Connection
Ethernet
Ethernet 2
如果在Windows 8或Server 2012或更新的操作系统上运行此命令,则可以使用
Get-NetAdapter
cmdlet来获得所需的结果:
Get-NetAdapter | Where {$_.Status -eq 'Up'} | Select -ExpandProperty Name
在较旧的操作系统上,您可以尝试这样做,即使用正则表达式特殊字符\s
将每个接口的文本拆分为单词“connected”,并将其尾随空格拆分,然后返回该拆分的第二项:
$Interfaces = netsh interface ipv4 show interface | where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
($_ -Split 'connected\s+')[1]
}
或者这里还有另一个选项,它避免使用-split
运算符,而是使用正则表达式查找来匹配“connected”,在我们要返回的内容之前,在字符串中后跟5个空格:
$Interfaces = netsh interface ipv4 show interface | where { $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
[regex]::matches($_, '(?<=connected\s{5})(.*)').value
}
$Interfaces=netsh interface ipv4 show interface |其中{$\不匹配'loopback'}
$Interfaces | ForEach对象{
[regex]::匹配($),(?如果您在Windows 8或Server 2012或更新的操作系统上运行此命令,则可以使用Get-NetAdapter
cmdlet来获得所需的结果:
Get-NetAdapter | Where {$_.Status -eq 'Up'} | Select -ExpandProperty Name
在较旧的操作系统上,您可以尝试这样做,即使用正则表达式特殊字符\s
将每个接口的文本拆分为单词“connected”,并将其尾随空格拆分,然后返回该拆分的第二项:
$Interfaces = netsh interface ipv4 show interface | where { $_ -match '\sconnected' -and $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
($_ -Split 'connected\s+')[1]
}
或者这里还有另一个选项,它避免使用-split
运算符,而是使用正则表达式查找来匹配“connected”,在我们要返回的内容之前,在字符串中后跟5个空格:
$Interfaces = netsh interface ipv4 show interface | where { $_ -notmatch 'loopback'}
$Interfaces | ForEach-Object {
[regex]::matches($_, '(?<=connected\s{5})(.*)').value
}
$Interfaces=netsh interface ipv4 show interface |其中{$\不匹配'loopback'}
$Interfaces | ForEach对象{
[regex]::匹配($),(?