2017-09-18 30 views
4

我正在使用一個函數調用另一個函數並返回一個PSObject數組或多個數組(我認爲)。每個循環對象之後的返回值是一組值。函數Test2中的$ a中的數據在下面,但它的計數是3,表示三個對象或數組,每個文件夾一個。這似乎很正常,如果我把它寫到一個CSV報表中,我會很好,但我正在尋找操縱每個數組中的數據。當我嘗試操縱數據時,它試圖操縱數組,並且我無法在每一行中搜索或使用項目。我也不知道我有多少個文件夾,所以解決方案需要具有通用性和可擴展性。我不知道如何輕鬆訪問所有數組中的每一行。PSObject數組數組返回Powershell讀取單個項目/行

Function Test1 { 
[cmdletbinding()] 
param(
    [Parameter(Position = 0, Mandatory = $true)] 
    [string]$Folder 
) 

$array1 = @("Folder1_Test1","Folder1_Test2","Folder1_Test3","Folder1_Test4","Folder1_Test5 ","Folder2_Test6","Folder2_Test7","Folder2_Test8","Folder2_Test9","Folder3_Test1 0") 
$array2 = @("Folder1_Test1","Folder1_Test4","Folder1_Test5","Folder2_Test9") 


$data = @() 
Foreach ($item in $array1) { 
    If ($item -match $Folder -and $array2 -notcontains $item) { 
     $Obj = New-Object PSObject -Property @{ 
      Folder = $Folder; 
      SubFolder = $item; 
      Message = "$item not found."; 
     } 
     $data += $Obj 
} 
} 
Return ,$data 
} 

Function Test2 { 
$Folders = @("Folder1", "Folder2", "Folder3") 
$a = $Folders | ForEach-Object {Test1 $_} 
$a.Count 

foreach ($item in $a) 
     { 
      $item.Folder 
      $item.SubFolder 
      $item.Message 
     } 
} 

$ A的輸出然而計數爲3

SubFolder  Message     Folder 
---------  -------     ------ 
Folder1_Test2 Folder1_Test2 not found. Folder1 
Folder1_Test3 Folder1_Test3 not found. Folder1 
Folder2_Test6 Folder2_Test6 not found. Folder2 
Folder2_Test7 Folder2_Test7 not found. Folder2 
Folder2_Test8 Folder2_Test8 not found. Folder2 
Folder3_Test10 Folder3_Test10 not found. Folder3 

我怎樣才能得到每個物體內部訪問的每一行?我希望能夠通過一個子文件夾進行搜索,然後確定它是在文件夾和寫郵件,是這樣的:提前

$a | ForEach-Object | Write-Host {"Subfolder $($_.Subfolder) is in $($_.Folder) and error message is $($_.Message)"} 

感謝。

回答

2

你正在創建的是一個包含三個元素的數組。數組中的每個元素都顯示信息。當你寫出來的conosole你看到所有的元素一起擠着:

SubFolder   Message      Folder 
---------   -------      ------ 
Folder1_Test2  Folder1_Test2 not found.  Folder1 
Folder1_Test3  Folder1_Test3 not found.  Folder1 
Folder1_Test5  Folder1_Test5  not found. Folder1 
Folder2_Test6  Folder2_Test6 not found.  Folder2 
Folder2_Test7  Folder2_Test7 not found.  Folder2 
Folder2_Test8  Folder2_Test8 not found.  Folder2 
Folder3_Test1 0 Folder3_Test1 0 not found. Folder3 

如果你看看一個$ [0]你會看到:

PS C:\WINDOWS\system32> $a[0] 

SubFolder   Message      Folder 
---------   -------      ------ 
Folder1_Test2  Folder1_Test2 not found.  Folder1 
Folder1_Test3  Folder1_Test3 not found.  Folder1 
Folder1_Test5  Folder1_Test5  not found. Folder1 

這就是爲什麼count返回3. 如果使用$a[0][0],您將看到一行,因爲它正在訪問$ a的第一個元素(它是一個數組),然後訪問該數組的第一個元素。您將不得不使用嵌套循環訪問嵌套數組中的每個元素。

+0

謝謝你解釋傑森。太簡單了,我試圖變得太花哨,但是謝謝你讓我直立。我用下面的代碼測試了它,並得到了我的結果。代碼回答。 – Nakious

0

感謝傑森我能夠得到的代碼工作。我在Test2的底部添加了這個,並且輸出如下,逐行。

Foreach ($Element in $a) { 
    ForEach ($item in $Element) { 
     Write-Host "Subfolder $($item.Subfolder) is in $($item.Folder) and 
error message is $($item.FolderMessage)" 
    } 


Subfolder Folder1_Test2 is in Folder1 and error message is Folder1_Test2 not found. 
Subfolder Folder1_Test3 is in Folder1 and error message is Folder1_Test3 not found. 
Subfolder Folder2_Test6 is in Folder2 and error message is Folder2_Test6 not found. 
Subfolder Folder2_Test7 is in Folder2 and error message is Folder2_Test7 not found. 
Subfolder Folder2_Test8 is in Folder2 and error message is Folder2_Test8 not found. 
Subfolder Folder3_Test10 is in Folder3 and error message is Folder3_Test10 not found.