2016-12-13 28 views
1

我有一個CSV文件,其中包含子網信息,我將用它來填充其中包含服務器信息的CSV文件。我開始通過導入子網信息,並在處理它時,我試圖將多個成員添加到初始哈希表,但它不像預期的那樣工作。 以下代碼按預期處理第一個項目,並使用正確信息創建一個新列。該代碼表明它至少處理了兩個以上的部分,但不添加成員。如何更改代碼以允許將多個成員創建爲單個數組?目標是讓每個子網的網關字段(列)對該子網唯一。將多個成員添加到powershell散列表

五個變量(變量A-E)的目的是模仿實際代碼中發生的事情。真正的代碼從哈希表運行比較,但這不是必需的。如果需要,我願意改變這部分。

CSV file contents: 
NetworkName,Subnet,VLANID,Gateway,VLAN 
Servers,"192.168.1.0/24","2041","192.168.1.1","ServerVLAN-2041" 
Workstations,"192.168.2.0/24","1001","192.168.2.1","WorkstationVLAN-1001" 
DMZ,"172.16.0.0/28","340","172.16.0.1","DMZVLAN-340" 
Servers,"192.168.3.0/24","2043","192.168.3.1","ServerVLAN-2043" 
Workstations,"192.168.4.0/24","1004","192.168.4.1","WorkstationVLAN-1004" 
DMZ,,,, 

CODE:

$csvfile = "C:\temp\testfile.csv" 
$hashArray = Import-CSV $csvfile 

$variableA = "192.168.1.0" 
$variableB = "192.168.2.0" 
$variableC = "192.168.3.0" 
$variableD = "172.16.0.1" 
$variableE = "192.168.5.0" 

$hashArray | % { 
if ($_.subnet) { $variable = ($_.subnet).split("/")[0] } 
Else { $variable = $null } 
if ($variable -eq $variableA -and $variable -ne $null) 
{ 
    $_ | add-member "ServerGW1" -NotePropertyValue $_.gateway 
    Write-Host "Added Server gateway 1: "$_.gateway -ForegroundColor Yellow 
} 

if ($variable -eq $variableC -and $variable -ne $null) 
{ 
    $_ | add-member "ServerGW2" -NotePropertyValue $_.gateway 
    Write-Host "Added Server gateway 2: "$_.gateway -ForegroundColor Yellow 
} 

if ($variable -eq $variableB -and $variable -ne $null) 
{ 
    $_ | add-member "WorkstationGW1" -NotePropertyValue $_.gateway 
    Write-Host "Added Workstation gateway 1: "$_.gateway -ForegroundColor Yellow 
} 

if ($variable -eq $variableD -and $variable -ne $null) 
{ 
    $_ | add-member "DMZGW1" -NotePropertyValue $_.gateway 
    Write-Host "Added DMZ gateway 1: "$_.gateway -ForegroundColor Yellow 
} 

if ($variable -eq $variableE -and $variable -ne $null) 
{ 
    $_ | add-member "WorkstationGW2" -NotePropertyValue $_.gateway 
    Write-Host "Added Workstation gateway 2: "$_.gateway -ForegroundColor Yellow 
} 
} 
$hashArray | Out-GridView 

出的GridView OUTPUT:

OutGrid Results

控制檯輸出:

Console output

預期輸出: Expected Output

+1

有可能是複製和粘貼錯誤。當滿足$ variableE和$ variableB的條件時,添加「WorkstationGW1」屬性。 – cezarypiatek

+0

@cezarypiatek是的,我在代碼中進行了更改。謝謝! – McKenning

+1

mklement0打敗了我,但你爲什麼要爲你的對象唯一命名的屬性?最佳做法是定義您的基礎對象類和成員變量,並使其保持靜態。 – dontlooknow

回答

4

Out-GridView使用來自第一個對象的屬性來呈現列。所有新列(ServerGW1除外)都缺失,因爲未在$ hashArray中的第一個對象中進行初始化。你可以用$空值初始化所有屬性的所有行或提供的屬性列表您的結果輸出之前選擇到Out-GridView

$hashArray | Select-Object NetworkName,Subnet,VLANID,Gateway,VLAN, ServerGW1, ServerGW2,WorkstationGW1,WorkstationGW2,DMZGW1 | Out-GridView 

初始化所有屬性:

$hashArray | % { 
$variable =if ($_.subnet) { ($_.subnet).split("/")[0] }Else { $null } 
$_ | add-member "ServerGW1" -NotePropertyValue $(if ($variable -eq $variableA){ $_.gateway}Else { $null }) 
$_ | add-member "ServerGW2" -NotePropertyValue $(if ($variable -eq $variableC){ $_.gateway}Else { $null }) 
$_ | add-member "WorkstationGW1" -NotePropertyValue $(if($variable -eq $variableB){ $_.gateway}Else { $null }) 
$_ | add-member "DMZGW1" -NotePropertyValue $(if ($variable -eq $variableD){ $_.gateway}Else { $null }) 
$_ | add-member "WorkstationGW2" -NotePropertyValue $(if ($variable -eq $variableE){ $_.gateway}Else { $null }) 
} 
$hashArray | Out-GridView 
+0

啊。所以理論上,$ hashArray具有所有的屬性,但Out-GridView不顯示它們? – McKenning

+0

這回答了被問到的問題。但是從mklement0開始,返回對象並不是哈希表。 –

+0

沒錯。所有屬性都已添加,但對於Out-GridView不可見。你可以在ISE中用調試器檢查$ hashArray – cezarypiatek

1

爲了補充cezarypiatek's helpful answer,它提供了關鍵的指針:

所有格式的cmdlet,包括Out-GridView決定基於第一輸入對象上顯示什麼屬性(列),所以要保證顯示所有感興趣的列,您必須確保(至少)第一個輸入對象包含所有感興趣的屬性。

考慮到這一點,這裏是你的方法的簡化版本,做的是:

$csvfile = "C:\temp\testfile.csv" 
$networks = Import-CSV $csvfile 

# Define the subnets and their property names as an ordered hashtable. 
$subnets = [ordered] @{ 
    '192.168.1.0' = 'ServerGW1' 
    '192.168.2.0' = 'ServerGW2' 
    '192.168.3.0' = 'WorkstationGW1' 
    '172.16.0.0' = 'DMZGW1' 
    '192.168.4.0' = 'WorkstationGW2' 
} 

# Add all properties of interest to the input objects, to ensure 
# that Out-GridView (or other formatting cmdlets) show them all. 
    # Construct an array of property names, where '*' stands for the original properties... 
$propNames = @('*') + [string[]] $subnets.Values 
    # ... and create augmented objects based on them. 
$networks = $networks | Select-Object -property $propNames 

$networks | % { 
    # See if the 'subnet' column has a value... 
    if ($subnet = if ($_.subnet) { ($_.subnet).split("/")[0] } else { $null }) { 
    # ... and, if so, see if a subnet name is defined for the part before '/' ... 
    if ($subnets.Contains($subnet)) { 
     # ... and, if so, fill the subnet-named property with the subnet address. 
     $_.($subnets.$subnet) = $subnet 
    } 
    } 
} 
  • Import-CSV不返回哈希表返回自定義對象[pscustomobject]實例)。

  • 代碼依賴於一個事實是,在表述的上下文中,你既可以分配給一個變量,並使用該分配的,如在有條件的,比如這裏的情況( if ($subnet = ...))。

+0

你的代碼對我來說是一個很大的改進,但我仍然得到相同的結果:只添加一個成員(第一個成員)。 – McKenning

+0

@McKenning:cezarypiatek的答案包含關鍵指針;請參閱我的更新,以獲得更精簡的解決方案,並將該指針考慮在內。 – mklement0

0

試試這個,這個例子是動態的,你剛剛修改$ hashvariable像你想(或負載$ hashvariable與文件,如果你想)

$csvfile ="C:\temp\testfile.csv" 
$hashArray = Import-CSV $csvfile 

$hashvariable=[ordered]@{"192.168.1.0"="ServerGW1"; "192.168.2.0"="WorkstationGW1"; "192.168.3.0"="ServerGW2"; "172.16.0.1"="DMZGW1"; "192.168.5.0"="WorkstationGW2" } 

$hashArray | 
%{ 
$result=$_ 
$variable = if ($_.subnet -ne $null) {($_.subnet).split("/")[0]} else {""} 
foreach ($key in $hashvariable.Keys) 
{ 
    $value=if ($variable -eq $key) {$key} else {""} 
    $result | add-member $hashvariable[$key] -NotePropertyValue $value 
} 

} 

$hashArray | Out-GridView