Parsing 使用powershell解析/格式化字符串

Parsing 使用powershell解析/格式化字符串,parsing,powershell,powershell-2.0,Parsing,Powershell,Powershell 2.0,这与powershell无关。我假设这些基本前提: 我们正在运行Windows7 我们想解析Windows7附带的systeminfo实用程序的结果 我们希望从输出中提取有关网络适配器的信息,并将其放入某些数据结构中进行进一步处理。 现在,我知道有比运行systeminfo和解析输出更好的方法来获取网卡信息。我的兴趣不在这个特殊的效用上。我正在考虑在powershell中解析/格式化某些文本的一般任务。我觉得powershell非常适合这类任务,根据具体要求,可能比C更好。这就是为什么我在pow

这与powershell无关。我假设这些基本前提:

我们正在运行Windows7 我们想解析Windows7附带的systeminfo实用程序的结果 我们希望从输出中提取有关网络适配器的信息,并将其放入某些数据结构中进行进一步处理。 现在,我知道有比运行systeminfo和解析输出更好的方法来获取网卡信息。我的兴趣不在这个特殊的效用上。我正在考虑在powershell中解析/格式化某些文本的一般任务。我觉得powershell非常适合这类任务,根据具体要求,可能比C更好。这就是为什么我在powershell中提出原始问题

但我的问题是。我总是觉得某些powershell语法有点麻烦。你将如何改进我的代码?请注意,代码不必在完全相同的结构中返回完全相同的结果。我正在寻找能够帮助文本解析/处理的技巧。我一次又一次地看到社区在处理许多任务时的独创性。我见过复杂问题的简单解决方案,在有人告诉你如何解决之前,这些解决方案根本不明显。这就是我问的原因

代码如下:

$networkCards = systeminfo | ForEach-Object {$a=0} {
    if ($_.startswith("Network Card(s)")) {$a=1} else {if ($a) {$_}}
}

$networkCards | ForEach-Object {$data=@{}} { 
    if ($_.trim().startswith("[")) {
        $c = $_.trim(); $data[$c] = @()} else {$data[$c] += $_.trim()
    } 
}

#Now we have a hash table with the keys as requested in the question 
#and the values are lists of separate strings, but those can be easily 
#concatenated if needed. Let's display it:
$data
首先,请停止使用Powershell作为常规脚本语言

PowerShell使用对象和解析字符串显然不是执行您想要的操作的好方法。我必须使您的脚本适应我的语言,才能在$data中获得一些内容

对我来说,这里的东西没有什么意义,但是你可以构造一个数组的哈希表,我给你一个利用它的方法

因此,要使用您可以写入的$data:

foreach ($key in $data.keys)
{
  write-Host ("The name is {0}, the value is {1}" -f $key, $data[$key])
}
foreach ($key in $data.keys)
{
  write-Host ("The name is {0}" -f $key)
  foreach($line in $data[$key])
  {
    Write-Host ("`t$line")
  }
}
就我而言,它给出了:

The name is [02]: fe80::391a:2817:8f3e:6f2f, the value is System.Object[]
The name is [02]: Intel(R) WiFi Link 5300 AGN, the value is System.Object[]
The name is [02]: fe80::c70:cc38:cf64:eb27, the value is System.Object[]
The name is [01]: 192.168.183.1, the value is System.Object[]
The name is [01]: 192.168.0.3, the value is System.Object[]
The name is [03]: VMware Virtual Ethernet Adapter for VMnet1, the value is System.Object[]
The name is [05]: Microsoft Virtual WiFi Miniport Adapter, the value is System.Object[]
The name is [02]: fe80::5d48:c4a:5987:ee73, the value is System.Object[]
The name is [04]: VMware Virtual Ethernet Adapter for VMnet8, the value is System.Object[]
The name is [01]: 192.168.234.1, the value is System.Object[]
The name is [01]: Intel(R) 82567LM Gigabit Network Connection, the value is System.Object[]
The name is [02]: fe80::391a:2817:8f3e:6f2f
The name is [02]: Intel(R) WiFi Link 5300 AGN
    Nom de la connexion : Connexion réseau sans fil
    État :                Support déconnecté
The name is [02]: fe80::c70:cc38:cf64:eb27
The name is [01]: 192.168.183.1
The name is [01]: 192.168.0.3
The name is [03]: VMware Virtual Ethernet Adapter for VMnet1
    Nom de la connexion : VMware Network Adapter VMnet1
    DHCP activé :         Non
    Adresse(s) IP
The name is [05]: Microsoft Virtual WiFi Miniport Adapter
    Nom de la connexion : Connexion réseau sans fil 2
    État :                Support déconnecté
The name is [02]: fe80::5d48:c4a:5987:ee73
The name is [04]: VMware Virtual Ethernet Adapter for VMnet8
    Nom de la connexion : VMware Network Adapter VMnet8
    DHCP activé :         Non
    Adresse(s) IP
The name is [01]: 192.168.234.1
The name is [01]: Intel(R) 82567LM Gigabit Network Connection
    Nom de la connexion : Connexion au réseau local
    DHCP activé :         Oui
    Serveur DHCP :        192.168.0.254
    Adresse(s) IP
因为$data[$key]是一个数组。你可以写:

foreach ($key in $data.keys)
{
  write-Host ("The name is {0}, the value is {1}" -f $key, $data[$key])
}
foreach ($key in $data.keys)
{
  write-Host ("The name is {0}" -f $key)
  foreach($line in $data[$key])
  {
    Write-Host ("`t$line")
  }
}
对我来说,它给出了:

The name is [02]: fe80::391a:2817:8f3e:6f2f, the value is System.Object[]
The name is [02]: Intel(R) WiFi Link 5300 AGN, the value is System.Object[]
The name is [02]: fe80::c70:cc38:cf64:eb27, the value is System.Object[]
The name is [01]: 192.168.183.1, the value is System.Object[]
The name is [01]: 192.168.0.3, the value is System.Object[]
The name is [03]: VMware Virtual Ethernet Adapter for VMnet1, the value is System.Object[]
The name is [05]: Microsoft Virtual WiFi Miniport Adapter, the value is System.Object[]
The name is [02]: fe80::5d48:c4a:5987:ee73, the value is System.Object[]
The name is [04]: VMware Virtual Ethernet Adapter for VMnet8, the value is System.Object[]
The name is [01]: 192.168.234.1, the value is System.Object[]
The name is [01]: Intel(R) 82567LM Gigabit Network Connection, the value is System.Object[]
The name is [02]: fe80::391a:2817:8f3e:6f2f
The name is [02]: Intel(R) WiFi Link 5300 AGN
    Nom de la connexion : Connexion réseau sans fil
    État :                Support déconnecté
The name is [02]: fe80::c70:cc38:cf64:eb27
The name is [01]: 192.168.183.1
The name is [01]: 192.168.0.3
The name is [03]: VMware Virtual Ethernet Adapter for VMnet1
    Nom de la connexion : VMware Network Adapter VMnet1
    DHCP activé :         Non
    Adresse(s) IP
The name is [05]: Microsoft Virtual WiFi Miniport Adapter
    Nom de la connexion : Connexion réseau sans fil 2
    État :                Support déconnecté
The name is [02]: fe80::5d48:c4a:5987:ee73
The name is [04]: VMware Virtual Ethernet Adapter for VMnet8
    Nom de la connexion : VMware Network Adapter VMnet8
    DHCP activé :         Non
    Adresse(s) IP
The name is [01]: 192.168.234.1
The name is [01]: Intel(R) 82567LM Gigabit Network Connection
    Nom de la connexion : Connexion au réseau local
    DHCP activé :         Oui
    Serveur DHCP :        192.168.0.254
    Adresse(s) IP

我将尝试缓和上面的答案:-:

尽管Powershell可用于解析来自例如systeminfo的信息,但有更好的信息接口。请查看Get-WmiObject。在这种情况下,您可能会想到获取WmiObject Win32_NetworkAdapter或Win32_NetworkAdapter配置。还有Win32_NetworkProtocol和Win32_NetworkConnection,它们可以为您提供所需的信息


与“选择对象”、“对对象排序”和“设置表格格式”相结合,以显示所需的数据。

所有谈论对象的人都是正确的。其思想是将一个对象从一个方法直接发射到另一个方法,然后对该对象进行操作。我不是PS的大师,但我开始明白了。希望这个脚本能让你前进一点

$text = @"
key1:value1
key2:value2
key3:value3
"@
$map = @{}
($text -split "`n") | Where-Object {$_ -imatch 'key2'} | foreach-object {$tokens = $_ -split ":"; $map.Add($tokens[0],$tokens[1])}
$map

谢谢你的贡献。请您详细说明一下,首先,请停止像往常一样使用Powershell脚本语言。我完全不明白你的意思。当你需要信息的时候。在PowerShell中,游戏是从cmdlet或大部分时间的.NET类中查找对象,这些类将通过其属性或方法向您提供对象。WMI也是一个很好的提供商。我非常熟悉powershell是什么类型的语言,但无论如何还是要感谢你=我将借用JPBlanc的答案,并说在powershell中解析文本时,游戏将创建对象。虽然这看起来可能比简单地重新格式化输入文本更复杂,但它简化了下游的一切。谢谢你=我知道WMI。我甚至明确表示,我知道有更好的方法可以获取问题中的信息,因为我知道人们会建议WMI=。这不是我现在感兴趣的。我只是使用systeminfo的输出作为可能需要解析的文本示例。因为我没有回答我的问题,这是因为问题的性质,我真的想结束这个问题,我选择了一个,我觉得这三个问题中最有用的一个。我想这与我在示例中所做的非常相似?或者你使用的是一个我忽略的概念?