2011-05-19 122 views
3

這個original question不是關於PowerShell的。我假設這些基本前提:用PowerShell解析/格式化字符串

  • 我們運行的是Windows 7
  • 我們要分析的是Windows 7附帶
  • 我們希望從輸出提取有關網絡適配器的信息和系統的系統信息工具的結果將其放入一些數據結構中以供進一步處理。

現在,我知道獲取網卡信息比運行systeminfo和解析輸出有更好的方法。我的興趣不在於這個特定的實用工具。我正在考慮在PowerShell中解析/格式化一些文本的通用任務。我覺得powershell非常適合這類任務,也許比C#更好(取決於特定的要求)。這就是爲什麼我提供my answer PowerShell的原始問題。

我的問題,雖然是這樣。我總覺得一些PowerShell的語法有點麻煩。 你會如何改進我的代碼?請注意,代碼不必在完全相同的結構中返回完全相同的結果。 我正在尋找可以幫助文本解析/處理的技巧。我一次又一次看到社區在處理大量任務方面的聰明才智。我已經看到了複雜問題的簡單解決方案,直到有人告訴你如何解決這些問題一點都不明顯。這就是我問的原因。

下面是代碼:

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

$networkCards | ForEach-Object {[email protected]{}} { 
    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 

回答

0

我會在上面緩和:-)答案試試:

雖然Powershell的可以用來解析信息從例如systeminfo,有更好的信息接口。看看Get-WmiObject。在這種情況下,似乎想到了Get-WmiObject Win32_NetworkAdapterWin32_NetworkAdapterConfiguration。也有Win32_NetworkProtocolWin32_NetworkConnection,可能會給你你需要的信息。

結合Select-Object,Sort-ObjectFormat-Table來展示您所需要的數據。

+0

謝謝=)我瞭解WMI。我甚至明確表示,我知道在我的問題中有更好的方法來獲取信息,因爲我知道人們會建議WMI =)。這不是我現在感興趣的東西。我只是用這個輸出的systeminfo作爲可能需要解析的文本的一個例子。 – 2011-05-19 09:11:08

+0

由於沒有答案我回答我的問題,(這是因爲問題的性質,真的),我想「關閉」這個問題,我選擇一個,我認爲這三個問題中最有用。 – 2011-05-22 21:45:02

1

首先,請停止使用PowerShell像往常一樣的腳本語言。

PowerShell使用對象和解析字符串是絕對不是做你想做的事情的好方法。我必須修改你的腳本到我的語言來得到$ data的東西

對於我來說這裏的東西沒有任何意義,但是你構造了一個數組的哈希表,並且我給你開發它的方法。

所以使用$數據,你可以寫:

foreach ($key in $data.keys) 
{ 
    write-Host ("The name is {0}, the value is {1}" -f $key, $data[$key]) 
} 

對我來說,它提供了:

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[] 

因爲$數據[$關鍵]是一個數組。你可以這樣寫:

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 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 
+0

謝謝你的貢獻。您能否詳細說明一下「首先,請停止使用Powershell作爲通常的腳本語言」。我完全和完全不明白你的意思。 – 2011-05-19 07:53:08

+0

當你需要一個信息。在Powershel中,遊戲是要找到對象(來自CmdLets或大部分時間.NET類),這些對象會根據他的屬性或方法提供給你。 WMI也是一個很好的提供商。 – JPBlanc 2011-05-19 08:15:30

+0

我對Powershell是什麼類型的語言非常熟悉,不過謝謝你=) – 2011-05-19 08:36:21

0

大家都在談論物體是否正確。這個想法是將一個對象從一個方法直接發送到另一個方法,然後對該對象進行操作。我不是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 
+0

這與我在我的例子中所做的很相似,我想呢?還是有一個你正在使用的概念,我忽略了? – 2011-05-19 20:41:00