2016-07-12 29 views
1

我有2個不同的數組,並且當我將它們列入腳本中時,第二個列的列不會出現。如果我單獨列出它們,那麼它們正確列出。PowerShell列出不同的數組

例如,在一個腳本上市的時候,我得到這樣的輸出:

Name      ipaddress numofconnections 
----      --------- ---------------- 
SRV1      12.2.2.2    0 
SRV2      11.1.1.1    0 
vServer1            
vServer2              

如果我單獨列出每個陣列中,我得到了這一點:

數組1

Name      ipaddress numofconnections 
----      --------- ---------------- 
SRV1      12.2.2.2    0 
SRV2      11.1.1.1    0 

Array2

name         curstate 
----         -------- 
vServer1       UP  
vServer2       DOWN  
+1

有什麼問題嗎? –

+1

我想你明確地選擇你想看到的特定列。 '$ Array1 |爲每個集合選擇Name,ipaddress,numofconnections'。或'format-table property1,property2,...'。 – n01d

+0

你目前如何列出它們? –

回答

0

如果我使用

| Out-String 

例如

$array1 | Out-String 
$array2 | Out-String 

然後它顯示原始帖子中的2個獨立表格

+0

爲什麼投票棄權。有效! 無論如何,我需要格式化表格並更改標題並遵循本文。 [鏈接](https://technet.microsoft.com/en-us/library/ee692794.aspx) 這整理了我的原始問題,我並不需要 外串 – mattnicola

0

我想PowerShell會獲取數組中的第一個對象並顯示它的屬性。如果列表中的另一個對象具有更多屬性,則不會顯示它們。但是,你可以遍歷項目,並選擇您的列:

$array1, $array2 | Foreach { $_ | select Name, ipaddress, curstate } 

輸出:

Name  ipaddress curstate 
----  --------- -------- 
SRV1  12.2.2.2   
SRV2  12.1.1.1   
vServer1   UP  
vServer2   DOWN  
0

PowerShell無法檢測到第一個和第二個數組之間的轉換,它只會看到一連串發送到管道的對象。

你可能會做這樣的事情:

$array1 = Get-Something 
$array2 = Get-SomeOtherThings 

$array1 # send to the pipeline 
$array2 # send to the pipeline (in this case it will append to the pipeline) 

發生這種情況時,PowerShell的將嘗試爲這些對象提供有效的展示,在這種情況下選擇的Format-Table佈局。這是通過查看收到的第一個對象並計算要顯示的屬性數來確定的。一旦選擇了顯示格式,它將對流水線中的所有對象使用相同的格式。

如果你不想顯示兩個不同的表,那麼你需要讓PowerShell知道兩個系列之間有一箇中斷。

這就是爲什麼answer by @mattnicola有效,因爲它將管道分成兩部分。

如果您wan't顯示的完全控制,那麼你可以這樣做,以及:

$array1 | Format-Table # causes the items in the pipeline being written to console in table layout (no items are emitted to the pipeline after this) 

$array2 | Format-Table # causes a new table to be written to the console.